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 #ifdef COMPILER2
  34 #include "opto/matcher.hpp"
  35 #endif
  36 
  37 // Release the CompiledICHolder* associated with this call site is there is one.
  38 void CompiledIC::cleanup_call_site(virtual_call_Relocation* call_site) {
  39   // This call site might have become stale so inspect it carefully.
  40   NativeCall* call = nativeCall_at(call_site->addr());
  41   if (is_icholder_entry(call->destination())) {
  42     NativeMovConstReg* value = nativeMovConstReg_at(call_site->cached_value());
  43     InlineCacheBuffer::queue_for_release((CompiledICHolder*)value->data());
  44   }
  45 }
  46 
  47 bool CompiledIC::is_icholder_call_site(virtual_call_Relocation* call_site) {
  48   // This call site might have become stale so inspect it carefully.
  49   NativeCall* call = nativeCall_at(call_site->addr());
  50   return is_icholder_entry(call->destination());
  51 }
  52 
  53 // ----------------------------------------------------------------------------
  54 
  55 #define __ _masm.
  56 void CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf) {
  57 #ifdef COMPILER2
  58   // Stub is fixed up when the corresponding call is converted from calling
  59   // compiled code to calling interpreted code.
  60   // set (empty), G5
  61   // jmp -1
  62 
  63   address mark = cbuf.insts_mark();  // Get mark within main instrs section.
  64 
  65   MacroAssembler _masm(&cbuf);
  66 
  67   address base =
  68   __ start_a_stub(to_interp_stub_size()*2);
  69   if (base == NULL) return;  // CodeBuffer::expand failed.
  70 
  71   // Static stub relocation stores the instruction address of the call.
  72   __ relocate(static_stub_Relocation::spec(mark));
  73 
  74   __ set_metadata(NULL, as_Register(Matcher::inline_cache_reg_encode()));
  75 
  76   __ set_inst_mark();
  77   AddressLiteral addrlit(-1);
  78   __ JUMP(addrlit, G3, 0);
  79 
  80   __ delayed()->nop();
  81 
  82   // Update current stubs pointer and restore code_end.
  83   __ end_a_stub();
  84 #else
  85   ShouldNotReachHere();
  86 #endif
  87 }
  88 #undef __
  89 
  90 int CompiledStaticCall::to_interp_stub_size() {
  91   // This doesn't need to be accurate but it must be larger or equal to
  92   // the real size of the stub.
  93   return (NativeMovConstReg::instruction_size +  // sethi/setlo;
  94           NativeJump::instruction_size + // sethi; jmp; nop
  95           (TraceJumps ? 20 * BytesPerInstWord : 0) );
  96 }
  97 
  98 // Relocation entries for call stub, compiled java to interpreter.
  99 int CompiledStaticCall::reloc_to_interp_stub() {
 100   return 10;  // 4 in emit_java_to_interp + 1 in Java_Static_Call
 101 }
 102 
 103 void CompiledStaticCall::set_to_interpreted(methodHandle callee, address entry) {
 104   address stub = find_stub();
 105   guarantee(stub != NULL, "stub not found");
 106 
 107   if (TraceICs) {
 108     ResourceMark rm;
 109     tty->print_cr("CompiledStaticCall@" INTPTR_FORMAT ": set_to_interpreted %s",
 110                   instruction_address(),
 111                   callee->name_and_sig_as_C_string());
 112   }
 113 
 114   // Creation also verifies the object.
 115   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 116   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
 117 
 118   assert(method_holder->data() == 0 || method_holder->data() == (intptr_t)callee(),
 119          "a) MT-unsafe modification of inline cache");
 120   assert(jump->jump_destination() == (address)-1 || jump->jump_destination() == entry,
 121          "b) MT-unsafe modification of inline cache");
 122 
 123   // Update stub.
 124   method_holder->set_data((intptr_t)callee());
 125   jump->set_jump_destination(entry);
 126 
 127   // Update jump to call.
 128   set_destination_mt_safe(stub);
 129 }
 130 
 131 void CompiledStaticCall::set_stub_to_clean(static_stub_Relocation* static_stub) {
 132   assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
 133   // Reset stub.
 134   address stub = static_stub->addr();
 135   assert(stub != NULL, "stub not found");
 136   // Creation also verifies the object.
 137   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 138   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
 139   method_holder->set_data(0);
 140   jump->set_jump_destination((address)-1);
 141 }
 142 
 143 //-----------------------------------------------------------------------------
 144 // Non-product mode code
 145 #ifndef PRODUCT
 146 
 147 void CompiledStaticCall::verify() {
 148   // Verify call.
 149   NativeCall::verify();
 150   if (os::is_MP()) {
 151     verify_alignment();
 152   }
 153 
 154   // Verify stub.
 155   address stub = find_stub();
 156   assert(stub != NULL, "no stub found for static call");
 157   // Creation also verifies the object.
 158   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 159   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
 160 
 161   // Verify state.
 162   assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
 163 }
 164 
 165 #endif // !PRODUCT