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