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_monitors;
140   bool          _has_wide_vector;
141 
142   MarkId        _next_call_type;
143   address       _invoke_mark_pc;
144 
145   CodeSection*  _instructions;
146   CodeSection*  _constants;
147 
148   OopRecorder*              _oop_recorder;
149   DebugInformationRecorder* _debug_recorder;
150   Dependencies*             _dependencies;
151   ExceptionHandlerTable     _exception_handler_table;
152   ImplicitExceptionTable    _implicit_exception_table;
153   bool                      _has_auto_box;
154 
155   static ConstantOopWriteValue* _oop_null_scope_value;
156   static ConstantIntValue*    _int_m1_scope_value;
157   static ConstantIntValue*    _int_0_scope_value;
158   static ConstantIntValue*    _int_1_scope_value;
159   static ConstantIntValue*    _int_2_scope_value;
160   static LocationValue*       _illegal_value;
161   static MarkerValue*         _virtual_byte_array_marker;
162 
163   jint pd_next_offset(NativeInstruction* inst, jint pc_offset, JVMCIObject method, JVMCI_TRAPS);
164   void pd_patch_OopConstant(int pc_offset, JVMCIObject constant, JVMCI_TRAPS);
165   void pd_patch_MetaspaceConstant(int pc_offset, JVMCIObject constant, JVMCI_TRAPS);
166   void pd_patch_DataSectionReference(int pc_offset, int data_offset, JVMCI_TRAPS);
167   void pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination, JVMCI_TRAPS);
168   void pd_relocate_JavaMethod(CodeBuffer &cbuf, JVMCIObject method, jint pc_offset, JVMCI_TRAPS);
169   void pd_relocate_poll(address pc, jint mark, JVMCI_TRAPS);
170 
171   JVMCIObjectArray sites()                { return _sites_handle; }
172   JVMCIPrimitiveArray code()              { return _code_handle; }
173   JVMCIPrimitiveArray  data_section()     { return _data_section_handle; }
174   JVMCIObjectArray data_section_patches() { return _data_section_patches_handle; }
175 #ifndef PRODUCT
176   JVMCIObjectArray comments()             { return _comments_handle; }
177 #endif
178   JVMCIObject word_kind()                 { return _word_kind_handle; }
179 
180 public:
181 
182   CodeInstaller(JVMCIEnv* jvmci_env) :
183     _arena(mtJVMCI),
184     _jvmci_env(jvmci_env),
185     _has_auto_box(false) {}
186 
187   JVMCI::CodeInstallResult install(JVMCICompiler* compiler,
188                                    JVMCIObject target,
189                                    JVMCIObject compiled_code,
190                                    CodeBlob*& cb,
191                                    nmethodLocker& nmethod_handle,
192                                    JVMCIObject installed_code,
193                                    FailedSpeculation** failed_speculations,
194                                    char* speculations,
195                                    int speculations_len,
196                                    JVMCI_TRAPS);
197 
198   JVMCIEnv* jvmci_env() { return _jvmci_env; }
199   JVMCIRuntime* runtime() { return _jvmci_env->runtime(); }
200 
201   static address runtime_call_target_address(oop runtime_call);
202   static VMReg get_hotspot_reg(jint jvmciRegisterNumber, JVMCI_TRAPS);
203   static bool is_general_purpose_reg(VMReg hotspotRegister);
204 
205   const OopMapSet* oopMapSet() const { return _debug_recorder->_oopmaps; }
206 
207 protected:
208   Location::Type get_oop_type(JVMCIObject value);
209   ScopeValue* get_scope_value(JVMCIObject value, BasicType type, GrowableArray<ScopeValue*>* objects, ScopeValue* &second, JVMCI_TRAPS);
210   MonitorValue* get_monitor_value(JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS);
211 
212   void* record_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS);
213 #ifdef _LP64
214   narrowKlass record_narrow_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS);
215 #endif
216 
217   // extract the fields of the HotSpotCompiledCode
218   void initialize_fields(JVMCIObject target, JVMCIObject compiled_code, JVMCI_TRAPS);
219   void initialize_dependencies(JVMCIObject compiled_code, OopRecorder* oop_recorder, JVMCI_TRAPS);
220 
221   int estimate_stubs_size(JVMCI_TRAPS);
222 
223   // perform data and call relocation on the CodeBuffer
224   JVMCI::CodeInstallResult initialize_buffer(CodeBuffer& buffer, bool check_size, JVMCI_TRAPS);
225 
226   void assumption_NoFinalizableSubclass(JVMCIObject assumption);
227   void assumption_ConcreteSubtype(JVMCIObject assumption);
228   void assumption_LeafType(JVMCIObject assumption);
229   void assumption_ConcreteMethod(JVMCIObject assumption);
230   void assumption_CallSiteTargetValue(JVMCIObject assumption, JVMCI_TRAPS);
231 
232   void site_Safepoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
233   void site_Infopoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
234   void site_Call(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
235   void site_DataPatch(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
236   void site_Mark(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
237   void site_ExceptionHandler(jint pc_offset, JVMCIObject site);
238 
239   OopMap* create_oop_map(JVMCIObject debug_info, JVMCI_TRAPS);
240 
241   VMReg getVMRegFromLocation(JVMCIObject location, int total_frame_size, JVMCI_TRAPS);
242 
243   /**
244    * Specifies the level of detail to record for a scope.
245    */
246   enum ScopeMode {
247     // Only record a method and BCI
248     BytecodePosition,
249     // Record a method, bci and JVM frame state
250     FullFrame
251   };
252 
253   int map_jvmci_bci(int bci);
254 
255   void record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS);
256   void record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, JVMCI_TRAPS) {
257     record_scope(pc_offset, debug_info, scope_mode, false /* is_mh_invoke */, false /* return_oop */, JVMCIENV);
258   }
259   void record_scope(jint pc_offset, JVMCIObject position, ScopeMode scope_mode, GrowableArray<ScopeValue*>* objects, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS);
260   void record_object_value(ObjectValue* sv, JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS);
261 
262   GrowableArray<ScopeValue*>* record_virtual_objects(JVMCIObject debug_info, JVMCI_TRAPS);
263 
264   int estimateStubSpace(int static_call_stubs);
265 };
266 
267 #endif // SHARE_JVMCI_JVMCICODEINSTALLER_HPP