1 /*
   2  * Copyright (c) 1999, 2007, 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 class SharkNativeWrapper : public SharkCompileInvariants {
  27   friend class SharkStackWithNativeFrame;
  28 
  29  public:
  30   static SharkNativeWrapper* build(SharkBuilder* builder,
  31                                    methodHandle  target,
  32                                    const char*   name,
  33                                    BasicType*    arg_types,
  34                                    BasicType     return_type) {
  35     return new SharkNativeWrapper(builder,
  36                                   target,
  37                                   name,
  38                                   arg_types,
  39                                   return_type);
  40   }
  41 
  42  private:
  43   SharkNativeWrapper(SharkBuilder* builder,
  44                      methodHandle  target,
  45                      const char*   name,
  46                      BasicType*    arg_types,
  47                      BasicType     return_type)
  48     : SharkCompileInvariants(NULL, builder),
  49       _target(target),
  50       _arg_types(arg_types),
  51       _return_type(return_type),
  52       _lock_slot_offset(0) { initialize(name); }
  53 
  54  private:
  55   void initialize(const char* name);
  56 
  57  private:
  58   methodHandle    _target;
  59   BasicType*      _arg_types;
  60   BasicType       _return_type;
  61   llvm::Function* _function;
  62   SharkStack*     _stack;
  63   llvm::Value*    _oop_tmp_slot;
  64   OopMapSet*      _oop_maps;
  65   int             _receiver_slot_offset;
  66   int             _lock_slot_offset;
  67 
  68   // The method being compiled.
  69  protected:
  70   methodHandle target() const {
  71     return _target;
  72   }
  73 
  74   // Properties of the method.
  75  protected:
  76   int arg_size() const {
  77     return target()->size_of_parameters();
  78   }
  79   BasicType arg_type(int i) const {
  80     return _arg_types[i];
  81   }
  82   BasicType return_type() const {
  83     return _return_type;
  84   }
  85   bool is_static() const {
  86     return target()->is_static();
  87   }
  88   bool is_synchronized() const {
  89     return target()->is_synchronized();
  90   }
  91   bool is_returning_oop() const {
  92     return target()->is_returning_oop();
  93   }
  94 
  95   // The LLVM function we are building.
  96  public:
  97   llvm::Function* function() const {
  98     return _function;
  99   }
 100 
 101   // The Zero stack and our frame on it.
 102  protected:
 103   SharkStack* stack() const {
 104     return _stack;
 105   }
 106 
 107   // Temporary oop storage.
 108  protected:
 109   llvm::Value* oop_tmp_slot() const {
 110     assert(is_static() || is_returning_oop(), "should be");
 111     return _oop_tmp_slot;
 112   }
 113 
 114   // Information required by nmethod::new_native_nmethod().
 115  public:
 116   int frame_size() const {
 117     return stack()->oopmap_frame_size();
 118   }
 119   ByteSize receiver_offset() const {
 120     return in_ByteSize(_receiver_slot_offset * wordSize);
 121   }
 122   ByteSize lock_offset() const {
 123     return in_ByteSize(_lock_slot_offset * wordSize);
 124   }
 125   OopMapSet* oop_maps() const {
 126     return _oop_maps;
 127   }
 128 
 129   // Helpers.
 130  private:
 131   llvm::BasicBlock* CreateBlock(const char* name = "") const {
 132     return llvm::BasicBlock::Create(SharkContext::current(), name, function());
 133   }
 134   llvm::Value* thread_state_address() const {
 135     return builder()->CreateAddressOfStructEntry(
 136       thread(), JavaThread::thread_state_offset(),
 137       llvm::PointerType::getUnqual(SharkType::jint_type()),
 138       "thread_state_address");
 139   }
 140   llvm::Value* pending_exception_address() const {
 141     return builder()->CreateAddressOfStructEntry(
 142       thread(), Thread::pending_exception_offset(),
 143       llvm::PointerType::getUnqual(SharkType::oop_type()),
 144       "pending_exception_address");
 145   }
 146   void CreateSetThreadState(JavaThreadState state) const {
 147     builder()->CreateStore(
 148       LLVMValue::jint_constant(state), thread_state_address());
 149   }
 150   void CreateWriteMemorySerializePage() const {
 151     builder()->CreateStore(
 152       LLVMValue::jint_constant(1),
 153       builder()->CreateIntToPtr(
 154         builder()->CreateAdd(
 155           LLVMValue::intptr_constant(
 156             (intptr_t) os::get_memory_serialize_page()),
 157           builder()->CreateAnd(
 158             builder()->CreateLShr(
 159               builder()->CreatePtrToInt(thread(), SharkType::intptr_type()),
 160               LLVMValue::intptr_constant(os::get_serialize_page_shift_count())),
 161             LLVMValue::intptr_constant(os::get_serialize_page_mask()))),
 162         llvm::PointerType::getUnqual(SharkType::jint_type())));
 163   }
 164   void CreateResetHandleBlock() const {
 165     llvm::Value *active_handles = builder()->CreateValueOfStructEntry(
 166       thread(),
 167       JavaThread::active_handles_offset(),
 168       SharkType::jniHandleBlock_type(),
 169       "active_handles");
 170     builder()->CreateStore(
 171       LLVMValue::intptr_constant(0),
 172       builder()->CreateAddressOfStructEntry(
 173         active_handles,
 174         in_ByteSize(JNIHandleBlock::top_offset_in_bytes()),
 175         llvm::PointerType::getUnqual(SharkType::intptr_type()),
 176         "top"));
 177   }
 178   llvm::LoadInst* CreateLoadPendingException() const {
 179     return builder()->CreateLoad(
 180       pending_exception_address(), "pending_exception");
 181   }
 182 };