1 /*
   2  * Copyright (c) 2011, 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 #ifndef SHARE_VM_JVMCI_JVMCI_CODE_INSTALLER_HPP
  25 #define SHARE_VM_JVMCI_JVMCI_CODE_INSTALLER_HPP
  26 
  27 #include "jvmci/jvmciCompiler.hpp"
  28 #include "jvmci/jvmciEnv.hpp"
  29 #include "code/nativeInst.hpp"
  30 
  31 #if INCLUDE_AOT
  32 class RelocBuffer : public StackObj {
  33   enum { stack_size = 1024 };
  34 public:
  35   RelocBuffer() : _size(0), _buffer(0) {}
  36   ~RelocBuffer();
  37   void ensure_size(size_t bytes);
  38   void set_size(size_t bytes);
  39   address begin() const;
  40   size_t size() const { return _size; }
  41 private:
  42   size_t _size;
  43   char _static_buffer[stack_size];
  44   char *_buffer;
  45 };
  46 
  47 class AOTOopRecorder : public OopRecorder {
  48 public:
  49   AOTOopRecorder(Arena* arena = NULL, bool deduplicate = false);
  50 
  51   virtual int find_index(Metadata* h);
  52   virtual int find_index(jobject h);
  53   int nr_meta_refs() const;
  54   jobject meta_element(int pos) const;
  55 
  56 private:
  57   void record_meta_ref(jobject ref, int index);
  58 
  59   GrowableArray<jobject>* _meta_refs;
  60 };
  61 
  62 class CodeMetadata {
  63 public:
  64   CodeMetadata() {}
  65 
  66   CodeBlob* get_code_blob() const { return _cb; }
  67 
  68   PcDesc* get_pc_desc() const { return _pc_desc; }
  69   int get_nr_pc_desc() const { return _nr_pc_desc; }
  70 
  71   u_char* get_scopes_desc() const { return _scopes_desc; }
  72   int get_scopes_size() const { return _nr_scopes_desc; }
  73 
  74   RelocBuffer* get_reloc_buffer() { return &_reloc_buffer; }
  75 
  76   AOTOopRecorder* get_oop_recorder() { return _oop_recorder; }
  77 
  78   ExceptionHandlerTable* get_exception_table() { return _exception_table; }
  79 
  80   void set_pc_desc(PcDesc* desc, int count) {
  81     _pc_desc = desc;
  82     _nr_pc_desc = count;
  83   }
  84 
  85   void set_scopes(u_char* scopes, int size) {
  86     _scopes_desc = scopes;
  87     _nr_scopes_desc = size;
  88   }
  89 
  90   void set_oop_recorder(AOTOopRecorder* recorder) {
  91     _oop_recorder = recorder;
  92   }
  93 
  94   void set_exception_table(ExceptionHandlerTable* table) {
  95     _exception_table = table;
  96   }
  97 
  98 private:
  99   CodeBlob* _cb;
 100   PcDesc* _pc_desc;
 101   int _nr_pc_desc;
 102 
 103   u_char* _scopes_desc;
 104   int _nr_scopes_desc;
 105 
 106   RelocBuffer _reloc_buffer;
 107   AOTOopRecorder* _oop_recorder;
 108   ExceptionHandlerTable* _exception_table;
 109 };
 110 #endif // INCLUDE_AOT
 111 
 112 /*
 113  * This class handles the conversion from a InstalledCode to a CodeBlob or an nmethod.
 114  */
 115 class CodeInstaller : public StackObj {
 116   friend class JVMCIVMStructs;
 117 private:
 118   enum MarkId {
 119     VERIFIED_ENTRY                         = 1,
 120     UNVERIFIED_ENTRY                       = 2,
 121     OSR_ENTRY                              = 3,
 122     EXCEPTION_HANDLER_ENTRY                = 4,
 123     DEOPT_HANDLER_ENTRY                    = 5,
 124     INVOKEINTERFACE                        = 6,
 125     INVOKEVIRTUAL                          = 7,
 126     INVOKESTATIC                           = 8,
 127     INVOKESPECIAL                          = 9,
 128     INLINE_INVOKE                          = 10,
 129     POLL_NEAR                              = 11,
 130     POLL_RETURN_NEAR                       = 12,
 131     POLL_FAR                               = 13,
 132     POLL_RETURN_FAR                        = 14,
 133     CARD_TABLE_ADDRESS                     = 15,
 134     CARD_TABLE_SHIFT                       = 16,
 135     HEAP_TOP_ADDRESS                       = 17,
 136     HEAP_END_ADDRESS                       = 18,
 137     NARROW_KLASS_BASE_ADDRESS              = 19,
 138     NARROW_OOP_BASE_ADDRESS                = 20,
 139     CRC_TABLE_ADDRESS                      = 21,
 140     LOG_OF_HEAP_REGION_GRAIN_BYTES         = 22,
 141     INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED = 23,
 142     INVOKE_INVALID                         = -1
 143   };
 144 
 145   Arena         _arena;
 146 
 147   jobject       _data_section_handle;
 148   jobject       _data_section_patches_handle;
 149   jobject       _sites_handle;
 150   CodeOffsets   _offsets;
 151 
 152   jobject       _code_handle;
 153   jint          _code_size;
 154   jint          _total_frame_size;
 155   jint          _orig_pc_offset;
 156   jint          _parameter_count;
 157   jint          _constants_size;
 158 #ifndef PRODUCT
 159   jobject       _comments_handle;
 160 #endif
 161 
 162   bool          _has_wide_vector;
 163   jobject       _word_kind_handle;
 164 
 165   MarkId        _next_call_type;
 166   address       _invoke_mark_pc;
 167 
 168   CodeSection*  _instructions;
 169   CodeSection*  _constants;
 170 
 171   OopRecorder*              _oop_recorder;
 172   DebugInformationRecorder* _debug_recorder;
 173   Dependencies*             _dependencies;
 174   ExceptionHandlerTable     _exception_handler_table;
 175 
 176   bool _immutable_pic_compilation;  // Installer is called for Immutable PIC compilation.
 177 
 178   static ConstantOopWriteValue* _oop_null_scope_value;
 179   static ConstantIntValue*    _int_m1_scope_value;
 180   static ConstantIntValue*    _int_0_scope_value;
 181   static ConstantIntValue*    _int_1_scope_value;
 182   static ConstantIntValue*    _int_2_scope_value;
 183   static LocationValue*       _illegal_value;
 184 
 185   jint pd_next_offset(NativeInstruction* inst, jint pc_offset, Handle method, TRAPS);
 186   void pd_patch_OopConstant(int pc_offset, Handle constant, TRAPS);
 187   void pd_patch_MetaspaceConstant(int pc_offset, Handle constant, TRAPS);
 188   void pd_patch_DataSectionReference(int pc_offset, int data_offset, TRAPS);
 189   void pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination, TRAPS);
 190   void pd_relocate_JavaMethod(CodeBuffer &cbuf, Handle method, jint pc_offset, TRAPS);
 191   void pd_relocate_poll(address pc, jint mark, TRAPS);
 192 
 193   objArrayOop sites();
 194   arrayOop code();
 195   arrayOop data_section();
 196   objArrayOop data_section_patches();
 197 #ifndef PRODUCT
 198   objArrayOop comments();
 199 #endif
 200 
 201   oop word_kind();
 202 
 203 public:
 204 
 205   CodeInstaller(bool immutable_pic_compilation) : _arena(mtCompiler), _immutable_pic_compilation(immutable_pic_compilation) {}
 206 
 207 #if INCLUDE_AOT
 208   JVMCIEnv::CodeInstallResult gather_metadata(Handle target, Handle compiled_code, CodeMetadata& metadata, TRAPS);
 209 #endif
 210   JVMCIEnv::CodeInstallResult install(JVMCICompiler* compiler, Handle target, Handle compiled_code, CodeBlob*& cb, Handle installed_code, Handle speculation_log, TRAPS);
 211 
 212   static address runtime_call_target_address(oop runtime_call);
 213   static VMReg get_hotspot_reg(jint jvmciRegisterNumber, TRAPS);
 214   static bool is_general_purpose_reg(VMReg hotspotRegister);
 215 
 216   const OopMapSet* oopMapSet() const { return _debug_recorder->_oopmaps; }
 217 
 218 protected:
 219   Location::Type get_oop_type(Thread* thread, Handle value);
 220   ScopeValue* get_scope_value(Handle value, BasicType type, GrowableArray<ScopeValue*>* objects, ScopeValue* &second, TRAPS);
 221   MonitorValue* get_monitor_value(Handle value, GrowableArray<ScopeValue*>* objects, TRAPS);
 222 
 223   void* record_metadata_reference(CodeSection* section, address dest, Handle constant, TRAPS);
 224 #ifdef _LP64
 225   narrowKlass record_narrow_metadata_reference(CodeSection* section, address dest, Handle constant, TRAPS);
 226 #endif
 227 
 228   // extract the fields of the HotSpotCompiledCode
 229   void initialize_fields(oop target, oop target_method, TRAPS);
 230   void initialize_dependencies(oop target_method, OopRecorder* oop_recorder, TRAPS);
 231 
 232   int estimate_stubs_size(TRAPS);
 233 
 234   // perform data and call relocation on the CodeBuffer
 235   JVMCIEnv::CodeInstallResult initialize_buffer(CodeBuffer& buffer, bool check_size, TRAPS);
 236 
 237   void assumption_NoFinalizableSubclass(Thread* thread, Handle assumption);
 238   void assumption_ConcreteSubtype(Thread* thread, Handle assumption);
 239   void assumption_LeafType(Thread* thread, Handle assumption);
 240   void assumption_ConcreteMethod(Thread* thread, Handle assumption);
 241   void assumption_CallSiteTargetValue(Thread* thread, Handle assumption);
 242 
 243   void site_Safepoint(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS);
 244   void site_Infopoint(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS);
 245   void site_Call(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS);
 246   void site_DataPatch(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS);
 247   void site_Mark(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS);
 248   void site_ExceptionHandler(jint pc_offset, Handle site);
 249 
 250   OopMap* create_oop_map(Handle debug_info, TRAPS);
 251 
 252   /**
 253    * Specifies the level of detail to record for a scope.
 254    */
 255   enum ScopeMode {
 256     // Only record a method and BCI
 257     BytecodePosition,
 258     // Record a method, bci and JVM frame state
 259     FullFrame
 260   };
 261 
 262   int map_jvmci_bci(int bci);
 263   void record_scope(jint pc_offset, Handle debug_info, ScopeMode scope_mode, bool return_oop, TRAPS);
 264   void record_scope(jint pc_offset, Handle debug_info, ScopeMode scope_mode, TRAPS) {
 265     record_scope(pc_offset, debug_info, scope_mode, false /* return_oop */, THREAD);
 266   }
 267   void record_scope(jint pc_offset, Handle position, ScopeMode scope_mode, GrowableArray<ScopeValue*>* objects, bool return_oop, TRAPS);
 268   void record_object_value(ObjectValue* sv, Handle value, GrowableArray<ScopeValue*>* objects, TRAPS);
 269 
 270   GrowableArray<ScopeValue*>* record_virtual_objects(Handle debug_info, TRAPS);
 271 
 272   int estimateStubSpace(int static_call_stubs);
 273 };
 274 
 275 /**
 276  * Gets the Method metaspace object from a HotSpotResolvedJavaMethodImpl Java object.
 277  */
 278 Method* getMethodFromHotSpotMethod(oop hotspot_method);
 279 
 280 
 281 
 282 #endif // SHARE_VM_JVMCI_JVMCI_CODE_INSTALLER_HPP