1 /*
   2  * Copyright (c) 2011, 2015, 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 class RelocBuffer : public StackObj {
  32   enum { stack_size = 1024 };
  33 public:
  34   RelocBuffer() : _size(0), _buffer(0) {}
  35   ~RelocBuffer();
  36   void ensure_size(size_t bytes);
  37   void set_size(size_t bytes);
  38   address begin() const;
  39   size_t size() const { return _size; }
  40 private:
  41   size_t _size;
  42   char _static_buffer[stack_size];
  43   char *_buffer;
  44 };
  45 
  46 class CodeMetadata {
  47 public:
  48   CodeMetadata() {}
  49 
  50   CodeBlob* get_code_blob() const { return _cb; }
  51 
  52   PcDesc* get_pc_desc() const { return _pc_desc; }
  53   int get_nr_pc_desc() const { return _nr_pc_desc; }
  54 
  55   u_char* get_scopes_desc() const { return _scopes_desc; }
  56   int get_scopes_size() const { return _nr_scopes_desc; }
  57 
  58   RelocBuffer* get_reloc_buffer() { return &_reloc_buffer; }
  59 
  60   ExceptionHandlerTable* get_exception_table() { return _exception_table; }
  61 
  62   void set_pc_desc(PcDesc* desc, int count) {
  63     _pc_desc = desc;
  64     _nr_pc_desc = count;
  65   }
  66 
  67   void set_scopes(u_char* scopes, int size) {
  68     _scopes_desc = scopes;
  69     _nr_scopes_desc = size;
  70   }
  71 
  72   void set_exception_table(ExceptionHandlerTable* table) {
  73     _exception_table = table;
  74   }
  75 
  76 private:
  77   CodeBlob* _cb;
  78   PcDesc* _pc_desc;
  79   int _nr_pc_desc;
  80 
  81   u_char* _scopes_desc;
  82   int _nr_scopes_desc;
  83 
  84   RelocBuffer _reloc_buffer;
  85   ExceptionHandlerTable* _exception_table;
  86 };
  87 
  88 /*
  89  * This class handles the conversion from a InstalledCode to a CodeBlob or an nmethod.
  90  */
  91 class CodeInstaller : public StackObj {
  92   friend class VMStructs;
  93 private:
  94   enum MarkId {
  95     VERIFIED_ENTRY             = 1,
  96     UNVERIFIED_ENTRY           = 2,
  97     OSR_ENTRY                  = 3,
  98     EXCEPTION_HANDLER_ENTRY    = 4,
  99     DEOPT_HANDLER_ENTRY        = 5,
 100     INVOKEINTERFACE            = 6,
 101     INVOKEVIRTUAL              = 7,
 102     INVOKESTATIC               = 8,
 103     INVOKESPECIAL              = 9,
 104     INLINE_INVOKE              = 10,
 105     POLL_NEAR                  = 11,
 106     POLL_RETURN_NEAR           = 12,
 107     POLL_FAR                   = 13,
 108     POLL_RETURN_FAR            = 14,
 109     CARD_TABLE_ADDRESS         = 15,
 110     CARD_TABLE_SHIFT           = 16,
 111     HEAP_TOP_ADDRESS           = 17,
 112     HEAP_END_ADDRESS           = 18,
 113     NARROW_KLASS_BASE_ADDRESS  = 19,
 114     CRC_TABLE_ADDRESS          = 20,
 115     INVOKE_INVALID             = -1
 116   };
 117 
 118   Arena         _arena;
 119 
 120   jobject       _data_section_handle;
 121   jobject       _data_section_patches_handle;
 122   jobject       _sites_handle;
 123   jobject       _exception_handlers_handle;
 124   CodeOffsets   _offsets;
 125 
 126   jobject       _code_handle;
 127   jint          _code_size;
 128   jint          _total_frame_size;
 129   jint          _custom_stack_area_offset;
 130   jint          _parameter_count;
 131   jint          _constants_size;
 132 #ifndef PRODUCT
 133   jobject       _comments_handle;
 134 #endif
 135 
 136   bool          _has_wide_vector;
 137   jobject       _word_kind_handle;
 138 
 139   MarkId        _next_call_type;
 140   address       _invoke_mark_pc;
 141 
 142   CodeSection*  _instructions;
 143   CodeSection*  _constants;
 144 
 145   OopRecorder*              _oop_recorder;
 146   DebugInformationRecorder* _debug_recorder;
 147   Dependencies*             _dependencies;
 148   ExceptionHandlerTable     _exception_handler_table;
 149 
 150   static ConstantOopWriteValue* _oop_null_scope_value;
 151   static ConstantIntValue*    _int_m1_scope_value;
 152   static ConstantIntValue*    _int_0_scope_value;
 153   static ConstantIntValue*    _int_1_scope_value;
 154   static ConstantIntValue*    _int_2_scope_value;
 155   static LocationValue*       _illegal_value;
 156 
 157   jint pd_next_offset(NativeInstruction* inst, jint pc_offset, Handle method, TRAPS);
 158   void pd_patch_OopConstant(int pc_offset, Handle constant, TRAPS);
 159   void pd_patch_MetaspaceConstant(int pc_offset, Handle constant, TRAPS);
 160   void pd_patch_DataSectionReference(int pc_offset, int data_offset);
 161   void pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination, TRAPS);
 162   void pd_relocate_JavaMethod(Handle method, jint pc_offset, TRAPS);
 163   void pd_relocate_poll(address pc, jint mark, TRAPS);
 164 
 165   objArrayOop sites() { return (objArrayOop) JNIHandles::resolve(_sites_handle); }
 166   arrayOop code() { return (arrayOop) JNIHandles::resolve(_code_handle); }
 167   arrayOop data_section() { return (arrayOop) JNIHandles::resolve(_data_section_handle); }
 168   objArrayOop data_section_patches() { return (objArrayOop) JNIHandles::resolve(_data_section_patches_handle); }
 169   objArrayOop exception_handlers() { return (objArrayOop) JNIHandles::resolve(_exception_handlers_handle); }
 170 #ifndef PRODUCT
 171   objArrayOop comments() { return (objArrayOop) JNIHandles::resolve(_comments_handle); }
 172 #endif
 173 
 174   oop word_kind() { return (oop) JNIHandles::resolve(_word_kind_handle); }
 175 
 176 public:
 177 
 178   CodeInstaller() : _arena(mtCompiler) {}
 179 
 180   JVMCIEnv::CodeInstallResult gather_metadata(Handle target, Handle& compiled_code, CodeMetadata& metadata, TRAPS);
 181   JVMCIEnv::CodeInstallResult install(JVMCICompiler* compiler, Handle target, Handle& compiled_code, CodeBlob*& cb, Handle installed_code, Handle speculation_log, TRAPS);
 182 
 183   static address runtime_call_target_address(oop runtime_call);
 184   static VMReg get_hotspot_reg(jint jvmciRegisterNumber, TRAPS);
 185   static bool is_general_purpose_reg(VMReg hotspotRegister);
 186 
 187   const OopMapSet* oopMapSet() const { return _debug_recorder->_oopmaps; }
 188 
 189 protected:
 190   Location::Type get_oop_type(Handle value);
 191   ScopeValue* get_scope_value(Handle value, BasicType type, GrowableArray<ScopeValue*>* objects, ScopeValue* &second, TRAPS);
 192   MonitorValue* get_monitor_value(Handle value, GrowableArray<ScopeValue*>* objects, TRAPS);
 193 
 194   Metadata* record_metadata_reference(Handle constant, TRAPS);
 195 #ifdef _LP64
 196   narrowKlass record_narrow_metadata_reference(Handle constant, TRAPS);
 197 #endif
 198 
 199   // extract the fields of the CompilationResult
 200   void initialize_fields(oop target, oop target_method, TRAPS);
 201   void initialize_dependencies(oop target_method, OopRecorder* oop_recorder, TRAPS);
 202 
 203   int estimate_stubs_size(TRAPS);
 204 
 205   // perform data and call relocation on the CodeBuffer
 206   JVMCIEnv::CodeInstallResult initialize_buffer(CodeBuffer& buffer, TRAPS);
 207 
 208   void assumption_NoFinalizableSubclass(Handle assumption);
 209   void assumption_ConcreteSubtype(Handle assumption);
 210   void assumption_LeafType(Handle assumption);
 211   void assumption_ConcreteMethod(Handle assumption);
 212   void assumption_CallSiteTargetValue(Handle assumption);
 213 
 214   void site_Safepoint(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS);
 215   void site_Infopoint(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS);
 216   void site_Call(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS);
 217   void site_DataPatch(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS);
 218   void site_Mark(CodeBuffer& buffer, jint pc_offset, Handle site, TRAPS);
 219 
 220   OopMap* create_oop_map(Handle debug_info, TRAPS);
 221 
 222   void record_scope(jint pc_offset, Handle debug_info, TRAPS);
 223   void record_scope(jint pc_offset, Handle code_pos, GrowableArray<ScopeValue*>* objects, TRAPS);
 224   void record_object_value(ObjectValue* sv, Handle value, GrowableArray<ScopeValue*>* objects, TRAPS);
 225 
 226   GrowableArray<ScopeValue*>* record_virtual_objects(Handle debug_info, TRAPS);
 227 
 228   void process_exception_handlers();
 229   int estimateStubSpace(int static_call_stubs);
 230 };
 231 
 232 /**
 233  * Gets the Method metaspace object from a HotSpotResolvedJavaMethodImpl Java object.
 234  */
 235 Method* getMethodFromHotSpotMethod(oop hotspot_method);
 236 
 237 
 238 
 239 #endif // SHARE_VM_JVMCI_JVMCI_CODE_INSTALLER_HPP