1 /*
  2  * Copyright (c) 2011, 2021, 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 #ifndef SHARE_JVMCI_JVMCICODEINSTALLER_HPP
 25 #define SHARE_JVMCI_JVMCICODEINSTALLER_HPP
 26 
 27 #include "code/debugInfoRec.hpp"
 28 #include "code/exceptionHandlerTable.hpp"
 29 #include "code/nativeInst.hpp"
 30 #include "jvmci/jvmci.hpp"
 31 #include "jvmci/jvmciEnv.hpp"
 32 
 33 class CodeMetadata {
 34 public:
 35   CodeMetadata() {}
 36 
 37   CodeBlob* get_code_blob() const { return _cb; }
 38 
 39   PcDesc* get_pc_desc() const { return _pc_desc; }
 40   int get_nr_pc_desc() const { return _nr_pc_desc; }
 41 
 42   u_char* get_scopes_desc() const { return _scopes_desc; }
 43   int get_scopes_size() const { return _nr_scopes_desc; }
 44 
 45   ExceptionHandlerTable* get_exception_table() { return _exception_table; }
 46 
 47   ImplicitExceptionTable* get_implicit_exception_table() { return _implicit_exception_table; }
 48 
 49   void set_pc_desc(PcDesc* desc, int count) {
 50     _pc_desc = desc;
 51     _nr_pc_desc = count;
 52   }
 53 
 54   void set_scopes(u_char* scopes, int size) {
 55     _scopes_desc = scopes;
 56     _nr_scopes_desc = size;
 57   }
 58 
 59   void set_exception_table(ExceptionHandlerTable* table) {
 60     _exception_table = table;
 61   }
 62 
 63   void set_implicit_exception_table(ImplicitExceptionTable* table) {
 64     _implicit_exception_table = table;
 65   }
 66 
 67 private:
 68   CodeBlob* _cb;
 69   PcDesc* _pc_desc;
 70   int _nr_pc_desc;
 71 
 72   u_char* _scopes_desc;
 73   int _nr_scopes_desc;
 74 
 75   ExceptionHandlerTable* _exception_table;
 76   ImplicitExceptionTable* _implicit_exception_table;
 77 };
 78 
 79 /*
 80  * This class handles the conversion from a InstalledCode to a CodeBlob or an nmethod.
 81  */
 82 class CodeInstaller : public StackObj {
 83   friend class JVMCIVMStructs;
 84 private:
 85   enum MarkId {
 86     INVALID_MARK,
 87     VERIFIED_ENTRY,
 88     UNVERIFIED_ENTRY,
 89     OSR_ENTRY,
 90     EXCEPTION_HANDLER_ENTRY,
 91     DEOPT_HANDLER_ENTRY,
 92     FRAME_COMPLETE,
 93     INVOKEINTERFACE,
 94     INVOKEVIRTUAL,
 95     INVOKESTATIC,
 96     INVOKESPECIAL,
 97     INLINE_INVOKE,
 98     POLL_NEAR,
 99     POLL_RETURN_NEAR,
100     POLL_FAR,
101     POLL_RETURN_FAR,
102     CARD_TABLE_ADDRESS,
103     CARD_TABLE_SHIFT,
104     HEAP_TOP_ADDRESS,
105     HEAP_END_ADDRESS,
106     NARROW_KLASS_BASE_ADDRESS,
107     NARROW_OOP_BASE_ADDRESS,
108     CRC_TABLE_ADDRESS,
109     LOG_OF_HEAP_REGION_GRAIN_BYTES,
110     INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED,
111     DEOPT_MH_HANDLER_ENTRY,
112     VERIFY_OOPS,
113     VERIFY_OOP_BITS,
114     VERIFY_OOP_MASK,
115     VERIFY_OOP_COUNT_ADDRESS,
116     INVOKE_INVALID                         = -1
117   };
118 
119   Arena         _arena;
120   JVMCIEnv*     _jvmci_env;
121 
122   JVMCIPrimitiveArray    _data_section_handle;
123   JVMCIObjectArray       _data_section_patches_handle;
124   JVMCIObjectArray       _sites_handle;
125 #ifndef PRODUCT
126   JVMCIObjectArray       _comments_handle;
127 #endif
128   JVMCIPrimitiveArray    _code_handle;
129   JVMCIObject            _word_kind_handle;
130 
131   CodeOffsets   _offsets;
132 
133   jint          _code_size;
134   jint          _total_frame_size;
135   jint          _orig_pc_offset;
136   jint          _parameter_count;
137   jint          _constants_size;
138 
139   bool          _has_wide_vector;
140 
141   MarkId        _next_call_type;
142   address       _invoke_mark_pc;
143 
144   CodeSection*  _instructions;
145   CodeSection*  _constants;
146 
147   OopRecorder*              _oop_recorder;
148   DebugInformationRecorder* _debug_recorder;
149   Dependencies*             _dependencies;
150   ExceptionHandlerTable     _exception_handler_table;
151   ImplicitExceptionTable    _implicit_exception_table;
152   bool                      _has_auto_box;
153 
154   static ConstantOopWriteValue* _oop_null_scope_value;
155   static ConstantIntValue*    _int_m1_scope_value;
156   static ConstantIntValue*    _int_0_scope_value;
157   static ConstantIntValue*    _int_1_scope_value;
158   static ConstantIntValue*    _int_2_scope_value;
159   static LocationValue*       _illegal_value;
160   static MarkerValue*         _virtual_byte_array_marker;
161 
162   jint pd_next_offset(NativeInstruction* inst, jint pc_offset, JVMCIObject method, JVMCI_TRAPS);
163   void pd_patch_OopConstant(int pc_offset, JVMCIObject constant, JVMCI_TRAPS);
164   void pd_patch_MetaspaceConstant(int pc_offset, JVMCIObject constant, JVMCI_TRAPS);
165   void pd_patch_DataSectionReference(int pc_offset, int data_offset, JVMCI_TRAPS);
166   void pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination, JVMCI_TRAPS);
167   void pd_relocate_JavaMethod(CodeBuffer &cbuf, JVMCIObject method, jint pc_offset, JVMCI_TRAPS);
168   void pd_relocate_poll(address pc, jint mark, JVMCI_TRAPS);
169 
170   JVMCIObjectArray sites()                { return _sites_handle; }
171   JVMCIPrimitiveArray code()              { return _code_handle; }
172   JVMCIPrimitiveArray  data_section()     { return _data_section_handle; }
173   JVMCIObjectArray data_section_patches() { return _data_section_patches_handle; }
174 #ifndef PRODUCT
175   JVMCIObjectArray comments()             { return _comments_handle; }
176 #endif
177   JVMCIObject word_kind()                 { return _word_kind_handle; }
178 
179 public:
180 
181   CodeInstaller(JVMCIEnv* jvmci_env) :
182     _arena(mtJVMCI),
183     _jvmci_env(jvmci_env),
184     _has_auto_box(false) {}
185 
186   JVMCI::CodeInstallResult install(JVMCICompiler* compiler,
187                                    JVMCIObject target,
188                                    JVMCIObject compiled_code,
189                                    CodeBlob*& cb,
190                                    nmethodLocker& nmethod_handle,
191                                    JVMCIObject installed_code,
192                                    FailedSpeculation** failed_speculations,
193                                    char* speculations,
194                                    int speculations_len,
195                                    JVMCI_TRAPS);
196 
197   JVMCIEnv* jvmci_env() { return _jvmci_env; }
198   JVMCIRuntime* runtime() { return _jvmci_env->runtime(); }
199 
200   static address runtime_call_target_address(oop runtime_call);
201   static VMReg get_hotspot_reg(jint jvmciRegisterNumber, JVMCI_TRAPS);
202   static bool is_general_purpose_reg(VMReg hotspotRegister);
203 
204   const OopMapSet* oopMapSet() const { return _debug_recorder->_oopmaps; }
205 
206 protected:
207   Location::Type get_oop_type(JVMCIObject value);
208   ScopeValue* get_scope_value(JVMCIObject value, BasicType type, GrowableArray<ScopeValue*>* objects, ScopeValue* &second, JVMCI_TRAPS);
209   MonitorValue* get_monitor_value(JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS);
210 
211   void* record_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS);
212 #ifdef _LP64
213   narrowKlass record_narrow_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS);
214 #endif
215 
216   // extract the fields of the HotSpotCompiledCode
217   void initialize_fields(JVMCIObject target, JVMCIObject compiled_code, JVMCI_TRAPS);
218   void initialize_dependencies(JVMCIObject compiled_code, OopRecorder* oop_recorder, JVMCI_TRAPS);
219 
220   int estimate_stubs_size(JVMCI_TRAPS);
221 
222   // perform data and call relocation on the CodeBuffer
223   JVMCI::CodeInstallResult initialize_buffer(CodeBuffer& buffer, bool check_size, JVMCI_TRAPS);
224 
225   void assumption_NoFinalizableSubclass(JVMCIObject assumption);
226   void assumption_ConcreteSubtype(JVMCIObject assumption);
227   void assumption_LeafType(JVMCIObject assumption);
228   void assumption_ConcreteMethod(JVMCIObject assumption);
229   void assumption_CallSiteTargetValue(JVMCIObject assumption, JVMCI_TRAPS);
230 
231   void site_Safepoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
232   void site_Infopoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
233   void site_Call(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
234   void site_DataPatch(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
235   void site_Mark(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
236   void site_ExceptionHandler(jint pc_offset, JVMCIObject site);
237 
238   OopMap* create_oop_map(JVMCIObject debug_info, JVMCI_TRAPS);
239 
240   VMReg getVMRegFromLocation(JVMCIObject location, int total_frame_size, JVMCI_TRAPS);
241 
242   /**
243    * Specifies the level of detail to record for a scope.
244    */
245   enum ScopeMode {
246     // Only record a method and BCI
247     BytecodePosition,
248     // Record a method, bci and JVM frame state
249     FullFrame
250   };
251 
252   int map_jvmci_bci(int bci);
253 
254   void record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS);
255   void record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, JVMCI_TRAPS) {
256     record_scope(pc_offset, debug_info, scope_mode, false /* is_mh_invoke */, false /* return_oop */, JVMCIENV);
257   }
258   void record_scope(jint pc_offset, JVMCIObject position, ScopeMode scope_mode, GrowableArray<ScopeValue*>* objects, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS);
259   void record_object_value(ObjectValue* sv, JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS);
260 
261   GrowableArray<ScopeValue*>* record_virtual_objects(JVMCIObject debug_info, JVMCI_TRAPS);
262 
263   int estimateStubSpace(int static_call_stubs);
264 };
265 
266 #endif // SHARE_JVMCI_JVMCICODEINSTALLER_HPP