1 /*
   2  * Copyright (c) 1997, 2013, 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 "asm/macroAssembler.inline.hpp"
  27 #include "code/compiledIC.hpp"
  28 #include "code/icBuffer.hpp"
  29 #include "code/nmethod.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "runtime/mutexLocker.hpp"
  32 #include "runtime/safepoint.hpp"
  33 
  34 // Release the CompiledICHolder* associated with this call site is there is one.
  35 void CompiledIC::cleanup_call_site(virtual_call_Relocation* call_site) {
  36   // This call site might have become stale so inspect it carefully.
  37   NativeCall* call = nativeCall_at(call_site->addr());
  38   if (is_icholder_entry(call->destination())) {
  39     NativeMovConstReg* value = nativeMovConstReg_at(call_site->cached_value());
  40     InlineCacheBuffer::queue_for_release((CompiledICHolder*)value->data());
  41   }
  42 }
  43 
  44 bool CompiledIC::is_icholder_call_site(virtual_call_Relocation* call_site) {
  45   // This call site might have become stale so inspect it carefully.
  46   NativeCall* call = nativeCall_at(call_site->addr());
  47   return is_icholder_entry(call->destination());
  48 }
  49 
  50 // ----------------------------------------------------------------------------
  51 
  52 #define __ _masm.
  53 address CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf, address mark) {
  54   // Stub is fixed up when the corresponding call is converted from
  55   // calling compiled code to calling interpreted code.
  56   // movq rmethod, 0
  57   // jmp -4 # to self
  58 
  59   // address mark = cbuf.insts_mark();  // Get mark within main instrs section.
  60 
  61   // Note that the code buffer's insts_mark is always relative to insts.
  62   // That's why we must use the macroassembler to generate a stub.
  63   MacroAssembler _masm(&cbuf);
  64 
  65   address base = __ start_a_stub(to_interp_stub_size());
  66   int offset = __ offset();
  67   if (base == NULL) {
  68     return NULL;  // CodeBuffer::expand failed
  69   }
  70   // static stub relocation stores the instruction address of the call
  71   __ relocate(static_stub_Relocation::spec(mark));
  72   // static stub relocation also tags the Method* in the code-stream.
  73   __ mov_metadata(rmethod, (Metadata*)NULL);
  74   __ movptr(rscratch1, 0);
  75   __ br(rscratch1);
  76 
  77   assert((__ offset() - offset) <= (int)to_interp_stub_size(), "stub too big");
  78   __ end_a_stub();
  79   return base;
  80 }
  81 #undef __
  82 
  83 int CompiledStaticCall::to_interp_stub_size() {
  84   return 7 * NativeInstruction::instruction_size;
  85 }
  86 
  87 // Relocation entries for call stub, compiled java to interpreter.
  88 int CompiledStaticCall::reloc_to_interp_stub() {
  89   return 4; // 3 in emit_to_interp_stub + 1 in emit_call
  90 }
  91 
  92 void CompiledStaticCall::set_to_interpreted(methodHandle callee, address entry) {
  93   address stub = find_stub();
  94   guarantee(stub != NULL, "stub not found");
  95 
  96   if (TraceICs) {
  97     ResourceMark rm;
  98     tty->print_cr("CompiledStaticCall@" INTPTR_FORMAT ": set_to_interpreted %s",
  99                   p2i(instruction_address()),
 100                   callee->name_and_sig_as_C_string());
 101   }
 102 
 103   // Creation also verifies the object.
 104   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 105 #ifndef PRODUCT
 106   NativeGeneralJump* jump = nativeGeneralJump_at(method_holder->next_instruction_address());
 107 
 108   assert(method_holder->data() == 0 || method_holder->data() == (intptr_t)callee(),
 109          "a) MT-unsafe modification of inline cache");
 110   assert(method_holder->data() == 0 || jump->jump_destination() == entry,
 111          "b) MT-unsafe modification of inline cache");
 112 #endif
 113   // Update stub.
 114   method_holder->set_data((intptr_t)callee());
 115   NativeGeneralJump::insert_unconditional(method_holder->next_instruction_address(), entry);
 116   ICache::invalidate_range(stub, to_interp_stub_size());
 117   // Update jump to call.
 118   set_destination_mt_safe(stub);
 119 }
 120 
 121 void CompiledStaticCall::set_stub_to_clean(static_stub_Relocation* static_stub) {
 122   assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
 123   // Reset stub.
 124   address stub = static_stub->addr();
 125   assert(stub != NULL, "stub not found");
 126   // Creation also verifies the object.
 127   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 128   method_holder->set_data(0);
 129 }
 130 
 131 //-----------------------------------------------------------------------------
 132 // Non-product mode code
 133 #ifndef PRODUCT
 134 
 135 void CompiledStaticCall::verify() {
 136   // Verify call.
 137   NativeCall::verify();
 138   if (os::is_MP()) {
 139     verify_alignment();
 140   }
 141 
 142   // Verify stub.
 143   address stub = find_stub();
 144   assert(stub != NULL, "no stub found for static call");
 145   // Creation also verifies the object.
 146   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 147   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
 148 
 149   // Verify state.
 150   assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
 151 }
 152 
 153 #endif // !PRODUCT