0 /*
1  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  *
22  */
23 
24 #ifndef SHARE_VM_CODE_ICBUFFER_HPP
25 #define SHARE_VM_CODE_ICBUFFER_HPP
26 
27 #include "asm/codeBuffer.hpp"
28 #include "code/stubs.hpp"
29 #include "interpreter/bytecodes.hpp"
30 #include "memory/allocation.hpp"
31 #include "runtime/safepointVerifiers.hpp"
32 #include "utilities/align.hpp"
33 #include "utilities/debug.hpp"
34 #include "utilities/macros.hpp"
35 
36 //
37 // For CompiledIC's:
38 //
39 // In cases where we do not have MT-safe state transformation,
40 // we go to a transition state, using ICStubs. At a safepoint,
41 // the inline caches are transferred from the transitional code:
42 //
43 //    instruction_address --> 01 set xxx_oop, Ginline_cache_klass
44 //                            23 jump_to Gtemp, yyyy
45 //                            4  nop
46 
47 class ICStub: public Stub {
48  private:
49   int                 _size;       // total size of the stub incl. code
50   address             _ic_site;    // points at call instruction of owning ic-buffer
51   /* stub code follows here */
52  protected:
53   friend class ICStubInterface;
54   // This will be called only by ICStubInterface
55   void    initialize(int size,
56                      CodeStrings strings)        { _size = size; _ic_site = NULL; }
57   void    finalize(); // called when a method is removed
58 
59   // General info
60   int     size() const                           { return _size; }
61   static  int code_size_to_size(int code_size)   { return align_up((int)sizeof(ICStub), CodeEntryAlignment) + code_size; }
62 
63  public:
64   // Creation
65   void set_stub(CompiledIC *ic, void* cached_value, address dest_addr);
66 
67   // Code info
68   address code_begin() const                     { return (address)this + align_up(sizeof(ICStub), CodeEntryAlignment); }
69   address code_end() const                       { return (address)this + size(); }
70 
71   // Call site info
72   address ic_site() const                        { return _ic_site; }
73   void    clear();
74   bool    is_empty() const                       { return _ic_site == NULL; }
75 
76   // stub info
77   address destination() const;  // destination of jump instruction
78   void* cached_value() const;   // cached_value for stub
79 
80   // Debugging
81   void    verify()            PRODUCT_RETURN;
82   void    print()             PRODUCT_RETURN;
83 
84   // Creation
85   friend ICStub* ICStub_from_destination_address(address destination_address);
86 };
87 
88 // ICStub Creation
89 inline ICStub* ICStub_from_destination_address(address destination_address) {
90   ICStub* stub = (ICStub*) (destination_address - align_up(sizeof(ICStub), CodeEntryAlignment));
91   #ifdef ASSERT
92   stub->verify();
93   #endif
94   return stub;
95 }
96 
97 #ifdef ASSERT
98 // The ICRefillVerifier class is a stack allocated RAII object used to
99 // detect if a failed IC transition that required IC stub refilling has
100 // been accidentally missed. It is up to the caller to in that case
101 // refill IC stubs.
102 class ICRefillVerifier: StackObj {
103   bool _refill_requested;
104   bool _refill_remembered;
105 
106  public:
107   ICRefillVerifier();
108   ~ICRefillVerifier();
109 
110   void request_refill() { _refill_requested = true; }
111   void request_remembered() { _refill_remembered = true; }
112 };
113 
114 // The ICRefillVerifierMark is used to set the thread's current
115 // ICRefillVerifier to a provided one. This is useful in particular
116 // when transitioning IC stubs in parallel and refilling from the
117 // master thread invoking the IC stub transitioning code.
118 class ICRefillVerifierMark: StackObj {
119  public:
120   ICRefillVerifierMark(ICRefillVerifier* verifier);
121   ~ICRefillVerifierMark();
122 };
123 #else
124 class ICRefillVerifier: StackObj {
125  public:
126   ICRefillVerifier() {}
127 };
128 class ICRefillVerifierMark: StackObj {
129  public:
130   ICRefillVerifierMark(ICRefillVerifier* verifier) {}
131 };
132 #endif
133 
134 class InlineCacheBuffer: public AllStatic {
135  private:
136   // friends
137   friend class ICStub;
138 
139   static int ic_stub_code_size();
140 
141   static StubQueue* _buffer;
142 
143   static CompiledICHolder* _pending_released;
144   static int _pending_count;
145 
146   static StubQueue* buffer()                         { return _buffer;         }
147 
148   static ICStub* new_ic_stub();
149 
150   // Machine-dependent implementation of ICBuffer
151   static void    assemble_ic_buffer_code(address code_begin, void* cached_value, address entry_point);
152   static address ic_buffer_entry_point  (address code_begin);
153   static void*   ic_buffer_cached_value (address code_begin);
154 
155  public:
156 
157     // Initialization; must be called before first usage
158   static void initialize();
159 
160   // Access
161   static bool contains(address instruction_address);
162 
163     // removes the ICStubs after backpatching
164   static void update_inline_caches();
165   static void refill_ic_stubs();
166 
167   // for debugging
168   static bool is_empty();
169 
170   static void release_pending_icholders();
171   static void queue_for_release(CompiledICHolder* icholder);
172   static int pending_icholder_count() { return _pending_count; }
173 
174   // New interface
175   static bool    create_transition_stub(CompiledIC *ic, void* cached_value, address entry);
176   static address ic_destination_for(CompiledIC *ic);
177   static void*   cached_value_for(CompiledIC *ic);
178 };
179 
180 #endif // SHARE_VM_CODE_ICBUFFER_HPP
--- EOF ---