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.
cleanup_call_site(virtual_call_Relocation * call_site)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 
is_icholder_call_site(virtual_call_Relocation * call_site)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.
emit_to_interp_stub(CodeBuffer & cbuf)56 address 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 = __ start_a_stub(to_interp_stub_size());
68   if (base == NULL) {
69     return NULL;  // CodeBuffer::expand failed.
70   }
71 
72   // Static stub relocation stores the instruction address of the call.
73   __ relocate(static_stub_Relocation::spec(mark));
74 
75   __ set_metadata(NULL, as_Register(Matcher::inline_cache_reg_encode()));
76 
77   __ set_inst_mark();
78   AddressLiteral addrlit(-1);
79   __ JUMP(addrlit, G3, 0);
80 
81   __ delayed()->nop();
82 
83   // Update current stubs pointer and restore code_end.
84   __ end_a_stub();
85   return base;
86 #else
87   ShouldNotReachHere();
88 #endif
89 }
90 #undef __
91 
to_interp_stub_size()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.
reloc_to_interp_stub()101 int CompiledStaticCall::reloc_to_interp_stub() {
102   return 10;  // 4 in emit_java_to_interp + 1 in Java_Static_Call
103 }
104 
set_to_interpreted(methodHandle callee,address entry)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                   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 
set_stub_to_clean(static_stub_Relocation * static_stub)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 
verify()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
168