1 /*
   2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2009 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef SHARE_VM_SHARK_SHARKNATIVEWRAPPER_HPP
  27 #define SHARE_VM_SHARK_SHARKNATIVEWRAPPER_HPP
  28 
  29 #include "runtime/handles.hpp"
  30 #include "shark/llvmHeaders.hpp"
  31 #include "shark/sharkBuilder.hpp"
  32 #include "shark/sharkContext.hpp"
  33 #include "shark/sharkInvariants.hpp"
  34 #include "shark/sharkStack.hpp"
  35 
  36 class SharkNativeWrapper : public SharkCompileInvariants {
  37   friend class SharkStackWithNativeFrame;
  38 
  39  public:
  40   static SharkNativeWrapper* build(SharkBuilder* builder,
  41                                    methodHandle  target,
  42                                    const char*   name,
  43                                    BasicType*    arg_types,
  44                                    BasicType     return_type) {
  45     return new SharkNativeWrapper(builder,
  46                                   target,
  47                                   name,
  48                                   arg_types,
  49                                   return_type);
  50   }
  51 
  52  private:
  53   SharkNativeWrapper(SharkBuilder* builder,
  54                      methodHandle  target,
  55                      const char*   name,
  56                      BasicType*    arg_types,
  57                      BasicType     return_type)
  58     : SharkCompileInvariants(NULL, builder),
  59       _target(target),
  60       _arg_types(arg_types),
  61       _return_type(return_type),
  62       _lock_slot_offset(0) { initialize(name); }
  63 
  64  private:
  65   void initialize(const char* name);
  66 
  67  private:
  68   methodHandle    _target;
  69   BasicType*      _arg_types;
  70   BasicType       _return_type;
  71   llvm::Function* _function;
  72   SharkStack*     _stack;
  73   llvm::Value*    _oop_tmp_slot;
  74   OopMapSet*      _oop_maps;
  75   int             _receiver_slot_offset;
  76   int             _lock_slot_offset;
  77 
  78   // The method being compiled.
  79  protected:
  80   methodHandle target() const {
  81     return _target;
  82   }
  83 
  84   // Properties of the method.
  85  protected:
  86   int arg_size() const {
  87     return target()->size_of_parameters();
  88   }
  89   BasicType arg_type(int i) const {
  90     return _arg_types[i];
  91   }
  92   BasicType return_type() const {
  93     return _return_type;
  94   }
  95   bool is_static() const {
  96     return target()->is_static();
  97   }
  98   bool is_synchronized() const {
  99     return target()->is_synchronized();
 100   }
 101   bool is_returning_oop() const {
 102     return target()->is_returning_oop();
 103   }
 104 
 105   // The LLVM function we are building.
 106  public:
 107   llvm::Function* function() const {
 108     return _function;
 109   }
 110 
 111   // The Zero stack and our frame on it.
 112  protected:
 113   SharkStack* stack() const {
 114     return _stack;
 115   }
 116 
 117   // Temporary oop storage.
 118  protected:
 119   llvm::Value* oop_tmp_slot() const {
 120     assert(is_static() || is_returning_oop(), "should be");
 121     return _oop_tmp_slot;
 122   }
 123 
 124   // Information required by nmethod::new_native_nmethod().
 125  public:
 126   int frame_size() const {
 127     return stack()->oopmap_frame_size();
 128   }
 129   ByteSize receiver_offset() const {
 130     return in_ByteSize(_receiver_slot_offset * wordSize);
 131   }
 132   ByteSize lock_offset() const {
 133     return in_ByteSize(_lock_slot_offset * wordSize);
 134   }
 135   OopMapSet* oop_maps() const {
 136     return _oop_maps;
 137   }
 138 
 139   // Helpers.
 140  private:
 141   llvm::BasicBlock* CreateBlock(const char* name = "") const {
 142     return llvm::BasicBlock::Create(SharkContext::current(), name, function());
 143   }
 144   llvm::Value* thread_state_address() const {
 145     return builder()->CreateAddressOfStructEntry(
 146       thread(), JavaThread::thread_state_offset(),
 147       llvm::PointerType::getUnqual(SharkType::jint_type()),
 148       "thread_state_address");
 149   }
 150   llvm::Value* pending_exception_address() const {
 151     return builder()->CreateAddressOfStructEntry(
 152       thread(), Thread::pending_exception_offset(),
 153       llvm::PointerType::getUnqual(SharkType::oop_type()),
 154       "pending_exception_address");
 155   }
 156   void CreateSetThreadState(JavaThreadState state) const {
 157     builder()->CreateStore(
 158       LLVMValue::jint_constant(state), thread_state_address());
 159   }
 160   void CreateWriteMemorySerializePage() const {
 161     builder()->CreateStore(
 162       LLVMValue::jint_constant(1),
 163       builder()->CreateIntToPtr(
 164         builder()->CreateAdd(
 165           LLVMValue::intptr_constant(
 166             (intptr_t) os::get_memory_serialize_page()),
 167           builder()->CreateAnd(
 168             builder()->CreateLShr(
 169               builder()->CreatePtrToInt(thread(), SharkType::intptr_type()),
 170               LLVMValue::intptr_constant(os::get_serialize_page_shift_count())),
 171             LLVMValue::intptr_constant(os::get_serialize_page_mask()))),
 172         llvm::PointerType::getUnqual(SharkType::jint_type())));
 173   }
 174   void CreateResetHandleBlock() const {
 175     llvm::Value *active_handles = builder()->CreateValueOfStructEntry(
 176       thread(),
 177       JavaThread::active_handles_offset(),
 178       SharkType::jniHandleBlock_type(),
 179       "active_handles");
 180     builder()->CreateStore(
 181       LLVMValue::intptr_constant(0),
 182       builder()->CreateAddressOfStructEntry(
 183         active_handles,
 184         in_ByteSize(JNIHandleBlock::top_offset_in_bytes()),
 185         llvm::PointerType::getUnqual(SharkType::intptr_type()),
 186         "top"));
 187   }
 188   llvm::LoadInst* CreateLoadPendingException() const {
 189     return builder()->CreateLoad(
 190       pending_exception_address(), "pending_exception");
 191   }
 192 };
 193 
 194 #endif // SHARE_VM_SHARK_SHARKNATIVEWRAPPER_HPP