1 /*
  2  * Copyright (c) 1997, 2018, 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 #ifndef SHARE_VM_CODE_ICBUFFER_HPP
 26 #define SHARE_VM_CODE_ICBUFFER_HPP
 27 
 28 #include "asm/codeBuffer.hpp"
 29 #include "code/stubs.hpp"
 30 #include "interpreter/bytecodes.hpp"
 31 #include "memory/allocation.hpp"
 32 #include "runtime/safepointVerifiers.hpp"
 33 #include "utilities/align.hpp"
 34 #include "utilities/debug.hpp"
 35 #include "utilities/macros.hpp"
 36 
 37 //
 38 // For CompiledIC's:
 39 //
 40 // In cases where we do not have MT-safe state transformation,
 41 // we go to a transition state, using ICStubs. At a safepoint,
 42 // the inline caches are transferred from the transitional code:
 43 //
 44 //    instruction_address --> 01 set xxx_oop, Ginline_cache_klass
 45 //                            23 jump_to Gtemp, yyyy
 46 //                            4  nop
 47 
 48 class ICStub: public Stub {
 49  private:
 50   int                 _size;       // total size of the stub incl. code
 51   address             _ic_site;    // points at call instruction of owning ic-buffer
 52   /* stub code follows here */
 53  protected:
 54   friend class ICStubInterface;
 55   // This will be called only by ICStubInterface
 56   void    initialize(int size,
 57                      CodeStrings strings)        { _size = size; _ic_site = NULL; }
 58   void    finalize(); // called when a method is removed
 59 
 60   // General info
 61   int     size() const                           { return _size; }
 62   static  int code_size_to_size(int code_size)   { return align_up((int)sizeof(ICStub), CodeEntryAlignment) + code_size; }
 63 
 64  public:
 65   // Creation
 66   void set_stub(CompiledIC *ic, void* cached_value, address dest_addr);
 67 
 68   // Code info
 69   address code_begin() const                     { return (address)this + align_up(sizeof(ICStub), CodeEntryAlignment); }
 70   address code_end() const                       { return (address)this + size(); }
 71 
 72   // Call site info
 73   address ic_site() const                        { return _ic_site; }
 74   void    clear();
 75   bool    is_empty() const                       { return _ic_site == NULL; }
 76 
 77   // stub info
 78   address destination() const;  // destination of jump instruction
 79   void* cached_value() const;   // cached_value for stub
 80 
 81   // Debugging
 82   void    verify()            PRODUCT_RETURN;
 83   void    print()             PRODUCT_RETURN;
 84 
 85   // Creation
 86   friend ICStub* ICStub_from_destination_address(address destination_address);
 87 };
 88 
 89 // ICStub Creation
 90 inline ICStub* ICStub_from_destination_address(address destination_address) {
 91   ICStub* stub = (ICStub*) (destination_address - align_up(sizeof(ICStub), CodeEntryAlignment));
 92   #ifdef ASSERT
 93   stub->verify();
 94   #endif
 95   return stub;
 96 }
 97 
 98 #ifdef ASSERT
 99 // The ICRefillVerifier class is a stack allocated RAII object used to
100 // detect if a failed IC transition that required IC stub refilling has
101 // been accidentally missed. It is up to the caller to in that case
102 // refill IC stubs.
103 class ICRefillVerifier: StackObj {
104   bool _refill_requested;
105   bool _refill_remembered;
106 
107  public:
108   ICRefillVerifier();
109   ~ICRefillVerifier();
110 
111   void request_refill() { _refill_requested = true; }
112   void request_remembered() { _refill_remembered = true; }
113 };
114 
115 // The ICRefillVerifierMark is used to set the thread's current
116 // ICRefillVerifier to a provided one. This is useful in particular
117 // when transitioning IC stubs in parallel and refilling from the
118 // master thread invoking the IC stub transitioning code.
119 class ICRefillVerifierMark: StackObj {
120 public:
121   ICRefillVerifierMark(ICRefillVerifier* verifier);
122   ~ICRefillVerifierMark();
123 };
124 #endif
125 
126 class InlineCacheBuffer: public AllStatic {
127  private:
128   // friends
129   friend class ICStub;
130 
131   static int ic_stub_code_size();
132 
133   static StubQueue* _buffer;
134 
135   static CompiledICHolder* _pending_released;
136   static int _pending_count;
137 
138   static StubQueue* buffer()                         { return _buffer;         }
139 
140   static ICStub* new_ic_stub();
141 
142   // Machine-dependent implementation of ICBuffer
143   static void    assemble_ic_buffer_code(address code_begin, void* cached_value, address entry_point);
144   static address ic_buffer_entry_point  (address code_begin);
145   static void*   ic_buffer_cached_value (address code_begin);
146 
147  public:
148 
149     // Initialization; must be called before first usage
150   static void initialize();
151 
152   // Access
153   static bool contains(address instruction_address);
154 
155     // removes the ICStubs after backpatching
156   static void update_inline_caches();
157   static void refill_ic_stubs();
158 
159   // for debugging
160   static bool is_empty();
161 
162   static void release_pending_icholders();
163   static void queue_for_release(CompiledICHolder* icholder);
164   static int pending_icholder_count() { return _pending_count; }
165 
166   // New interface
167   static bool    create_transition_stub(CompiledIC *ic, void* cached_value, address entry);
168   static address ic_destination_for(CompiledIC *ic);
169   static void*   cached_value_for(CompiledIC *ic);
170 };
171 
172 #endif // SHARE_VM_CODE_ICBUFFER_HPP