1 /*
   2  * Copyright (c) 2011, 2017, 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_COMPILER_TO_VM_HPP
  25 #define SHARE_VM_JVMCI_JVMCI_COMPILER_TO_VM_HPP
  26 
  27 #include "jni.h"
  28 #include "runtime/javaCalls.hpp"
  29 #include "jvmci/jvmciJavaClasses.hpp"
  30 
  31 class CompilerToVM {
  32  public:
  33   class Data {
  34     friend class JVMCIVMStructs;
  35 
  36    private:
  37     static int Klass_vtable_start_offset;
  38     static int Klass_vtable_length_offset;
  39 
  40     static int Method_extra_stack_entries;
  41 
  42     static address SharedRuntime_ic_miss_stub;
  43     static address SharedRuntime_handle_wrong_method_stub;
  44     static address SharedRuntime_deopt_blob_unpack;
  45     static address SharedRuntime_deopt_blob_uncommon_trap;
  46 
  47     static size_t ThreadLocalAllocBuffer_alignment_reserve;
  48 
  49     static CollectedHeap* Universe_collectedHeap;
  50     static int Universe_base_vtable_size;
  51     static address Universe_narrow_oop_base;
  52     static int Universe_narrow_oop_shift;
  53     static address Universe_narrow_klass_base;
  54     static int Universe_narrow_klass_shift;
  55     static uintptr_t Universe_verify_oop_mask;
  56     static uintptr_t Universe_verify_oop_bits;
  57     static void* Universe_non_oop_bits;
  58 
  59     static bool _supports_inline_contig_alloc;
  60     static HeapWord** _heap_end_addr;
  61     static HeapWord* volatile* _heap_top_addr;
  62     static int _max_oop_map_stack_offset;
  63 
  64     static jbyte* cardtable_start_address;
  65     static int cardtable_shift;
  66 
  67     static int vm_page_size;
  68 
  69     static int sizeof_vtableEntry;
  70     static int sizeof_ExceptionTableElement;
  71     static int sizeof_LocalVariableTableElement;
  72     static int sizeof_ConstantPool;
  73     static int sizeof_SymbolPointer;
  74     static int sizeof_narrowKlass;
  75     static int sizeof_arrayOopDesc;
  76     static int sizeof_BasicLock;
  77 
  78     static address dsin;
  79     static address dcos;
  80     static address dtan;
  81     static address dexp;
  82     static address dlog;
  83     static address dlog10;
  84     static address dpow;
  85 
  86     static address symbol_init;
  87     static address symbol_clinit;
  88 
  89    public:
  90     static void initialize(TRAPS);
  91 
  92     static int max_oop_map_stack_offset() {
  93       assert(_max_oop_map_stack_offset > 0, "must be initialized");
  94       return Data::_max_oop_map_stack_offset;
  95     }
  96   };
  97 
  98   static bool cstring_equals(const char* const& s0, const char* const& s1) {
  99     return strcmp(s0, s1) == 0;
 100   }
 101 
 102   static unsigned cstring_hash(const char* const& s) {
 103     int h = 0;
 104     const char* p = s;
 105     while (*p != '\0') {
 106       h = 31 * h + *p;
 107       p++;
 108     }
 109     return h;
 110   }
 111 
 112   static JNINativeMethod methods[];
 113 
 114   static objArrayHandle initialize_intrinsics(TRAPS);
 115  public:
 116   static int methods_count();
 117 
 118   static inline Method* asMethod(jobject jvmci_method) {
 119     return (Method*) (address) HotSpotResolvedJavaMethodImpl::metaspaceMethod(jvmci_method);
 120   }
 121 
 122   static inline Method* asMethod(Handle jvmci_method) {
 123     return (Method*) (address) HotSpotResolvedJavaMethodImpl::metaspaceMethod(jvmci_method);
 124   }
 125 
 126   static inline Method* asMethod(oop jvmci_method) {
 127     return (Method*) (address) HotSpotResolvedJavaMethodImpl::metaspaceMethod(jvmci_method);
 128   }
 129 
 130   static inline ConstantPool* asConstantPool(jobject jvmci_constant_pool) {
 131     return (ConstantPool*) (address) HotSpotConstantPool::metaspaceConstantPool(jvmci_constant_pool);
 132   }
 133 
 134   static inline ConstantPool* asConstantPool(Handle jvmci_constant_pool) {
 135     return (ConstantPool*) (address) HotSpotConstantPool::metaspaceConstantPool(jvmci_constant_pool);
 136   }
 137 
 138   static inline ConstantPool* asConstantPool(oop jvmci_constant_pool) {
 139     return (ConstantPool*) (address) HotSpotConstantPool::metaspaceConstantPool(jvmci_constant_pool);
 140   }
 141 
 142   static inline Klass* asKlass(jobject jvmci_type) {
 143     return java_lang_Class::as_Klass(HotSpotResolvedObjectTypeImpl::javaClass(jvmci_type));
 144   }
 145 
 146   static inline Klass* asKlass(Handle jvmci_type) {
 147     return java_lang_Class::as_Klass(HotSpotResolvedObjectTypeImpl::javaClass(jvmci_type));
 148   }
 149 
 150   static inline Klass* asKlass(oop jvmci_type) {
 151     return java_lang_Class::as_Klass(HotSpotResolvedObjectTypeImpl::javaClass(jvmci_type));
 152   }
 153 
 154   static inline Klass* asKlass(jlong metaspaceKlass) {
 155     return (Klass*) (address) metaspaceKlass;
 156   }
 157 
 158   static inline MethodData* asMethodData(jlong metaspaceMethodData) {
 159     return (MethodData*) (address) metaspaceMethodData;
 160   }
 161 
 162   static oop get_jvmci_method(const methodHandle& method, TRAPS);
 163 
 164   static oop get_jvmci_type(Klass* klass, TRAPS);
 165 };
 166 
 167 class JavaArgumentUnboxer : public SignatureIterator {
 168  protected:
 169   JavaCallArguments*  _jca;
 170   arrayOop _args;
 171   int _index;
 172 
 173   Handle next_arg(BasicType expectedType) {
 174     assert(_index < _args->length(), "out of bounds");
 175     oop arg=((objArrayOop) (_args))->obj_at(_index++);
 176     assert(expectedType == T_OBJECT || java_lang_boxing_object::is_instance(arg, expectedType), "arg type mismatch");
 177     return Handle(Thread::current(), arg);
 178   }
 179 
 180  public:
 181   JavaArgumentUnboxer(Symbol* signature, JavaCallArguments*  jca, arrayOop args, bool is_static) : SignatureIterator(signature) {
 182     this->_return_type = T_ILLEGAL;
 183     _jca = jca;
 184     _index = 0;
 185     _args = args;
 186     if (!is_static) {
 187       _jca->push_oop(next_arg(T_OBJECT));
 188     }
 189     iterate();
 190     assert(_index == args->length(), "arg count mismatch with signature");
 191   }
 192 
 193   inline void do_bool()   { if (!is_return_type()) _jca->push_int(next_arg(T_BOOLEAN)->bool_field(java_lang_boxing_object::value_offset_in_bytes(T_BOOLEAN))); }
 194   inline void do_char()   { if (!is_return_type()) _jca->push_int(next_arg(T_CHAR)->char_field(java_lang_boxing_object::value_offset_in_bytes(T_CHAR))); }
 195   inline void do_short()  { if (!is_return_type()) _jca->push_int(next_arg(T_SHORT)->short_field(java_lang_boxing_object::value_offset_in_bytes(T_SHORT))); }
 196   inline void do_byte()   { if (!is_return_type()) _jca->push_int(next_arg(T_BYTE)->byte_field(java_lang_boxing_object::value_offset_in_bytes(T_BYTE))); }
 197   inline void do_int()    { if (!is_return_type()) _jca->push_int(next_arg(T_INT)->int_field(java_lang_boxing_object::value_offset_in_bytes(T_INT))); }
 198 
 199   inline void do_long()   { if (!is_return_type()) _jca->push_long(next_arg(T_LONG)->long_field(java_lang_boxing_object::value_offset_in_bytes(T_LONG))); }
 200   inline void do_float()  { if (!is_return_type()) _jca->push_float(next_arg(T_FLOAT)->float_field(java_lang_boxing_object::value_offset_in_bytes(T_FLOAT))); }
 201   inline void do_double() { if (!is_return_type()) _jca->push_double(next_arg(T_DOUBLE)->double_field(java_lang_boxing_object::value_offset_in_bytes(T_DOUBLE))); }
 202 
 203   inline void do_object() { _jca->push_oop(next_arg(T_OBJECT)); }
 204   inline void do_object(int begin, int end) { if (!is_return_type()) _jca->push_oop(next_arg(T_OBJECT)); }
 205   inline void do_array(int begin, int end)  { if (!is_return_type()) _jca->push_oop(next_arg(T_OBJECT)); }
 206   inline void do_void()                     { }
 207 };
 208 
 209 class JNIHandleMark : public StackObj {
 210   public:
 211     JNIHandleMark() { push_jni_handle_block(); }
 212     ~JNIHandleMark() { pop_jni_handle_block(); }
 213 
 214   private:
 215     static void push_jni_handle_block();
 216     static void pop_jni_handle_block();
 217 };
 218 
 219 #endif // SHARE_VM_JVMCI_JVMCI_COMPILER_TO_VM_HPP