1 /*
   2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2009, 2010 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_SHARKCONTEXT_HPP
  27 #define SHARE_VM_SHARK_SHARKCONTEXT_HPP
  28 
  29 #include "shark/llvmHeaders.hpp"
  30 #include "shark/sharkCompiler.hpp"
  31 
  32 // The LLVMContext class allows multiple instances of LLVM to operate
  33 // independently of each other in a multithreaded context.  We extend
  34 // this here to store things in Shark that are LLVMContext-specific.
  35 
  36 class SharkFreeQueueItem;
  37 
  38 class SharkContext : public llvm::LLVMContext {
  39  public:
  40   SharkContext(const char* name);
  41 
  42  private:
  43   llvm::Module* _module;
  44 
  45 #if SHARK_LLVM_VERSION >= 27
  46  public:
  47 #else
  48  private:
  49 #endif
  50   llvm::Module* module() const {
  51     return _module;
  52   }
  53 
  54   // Get this thread's SharkContext
  55  public:
  56   static SharkContext& current() {
  57     return *SharkCompiler::compiler()->context();
  58   }
  59 
  60   // Module accessors
  61  public:
  62 #if SHARK_LLVM_VERSION < 27
  63   llvm::ModuleProvider* module_provider() const {
  64     return new llvm::ExistingModuleProvider(module());
  65   }
  66 #endif
  67   void add_function(llvm::Function* function) const {
  68     module()->getFunctionList().push_back(function);
  69   }
  70   llvm::Constant* get_external(const char*               name,
  71                                const llvm::FunctionType* sig) {
  72     return module()->getOrInsertFunction(name, sig);
  73   }
  74 
  75   // Basic types
  76  private:
  77   const llvm::Type*        _void_type;
  78   const llvm::IntegerType* _bit_type;
  79   const llvm::IntegerType* _jbyte_type;
  80   const llvm::IntegerType* _jshort_type;
  81   const llvm::IntegerType* _jint_type;
  82   const llvm::IntegerType* _jlong_type;
  83   const llvm::Type*        _jfloat_type;
  84   const llvm::Type*        _jdouble_type;
  85 
  86  public:
  87   const llvm::Type* void_type() const {
  88     return _void_type;
  89   }
  90   const llvm::IntegerType* bit_type() const {
  91     return _bit_type;
  92   }
  93   const llvm::IntegerType* jbyte_type() const {
  94     return _jbyte_type;
  95   }
  96   const llvm::IntegerType* jshort_type() const {
  97     return _jshort_type;
  98   }
  99   const llvm::IntegerType* jint_type() const {
 100     return _jint_type;
 101   }
 102   const llvm::IntegerType* jlong_type() const {
 103     return _jlong_type;
 104   }
 105   const llvm::Type* jfloat_type() const {
 106     return _jfloat_type;
 107   }
 108   const llvm::Type* jdouble_type() const {
 109     return _jdouble_type;
 110   }
 111   const llvm::IntegerType* intptr_type() const {
 112     return LP64_ONLY(jlong_type()) NOT_LP64(jint_type());
 113   }
 114 
 115   // Compound types
 116  private:
 117   const llvm::PointerType*  _itableOffsetEntry_type;
 118   const llvm::PointerType*  _jniEnv_type;
 119   const llvm::PointerType*  _jniHandleBlock_type;
 120   const llvm::PointerType*  _klass_type;
 121   const llvm::PointerType*  _methodOop_type;
 122   const llvm::ArrayType*    _monitor_type;
 123   const llvm::PointerType*  _oop_type;
 124   const llvm::PointerType*  _thread_type;
 125   const llvm::PointerType*  _zeroStack_type;
 126   const llvm::FunctionType* _entry_point_type;
 127   const llvm::FunctionType* _osr_entry_point_type;
 128 
 129  public:
 130   const llvm::PointerType* itableOffsetEntry_type() const {
 131     return _itableOffsetEntry_type;
 132   }
 133   const llvm::PointerType* jniEnv_type() const {
 134     return _jniEnv_type;
 135   }
 136   const llvm::PointerType* jniHandleBlock_type() const {
 137     return _jniHandleBlock_type;
 138   }
 139   const llvm::PointerType* klass_type() const {
 140     return _klass_type;
 141   }
 142   const llvm::PointerType* methodOop_type() const {
 143     return _methodOop_type;
 144   }
 145   const llvm::ArrayType* monitor_type() const {
 146     return _monitor_type;
 147   }
 148   const llvm::PointerType* oop_type() const {
 149     return _oop_type;
 150   }
 151   const llvm::PointerType* thread_type() const {
 152     return _thread_type;
 153   }
 154   const llvm::PointerType* zeroStack_type() const {
 155     return _zeroStack_type;
 156   }
 157   const llvm::FunctionType* entry_point_type() const {
 158     return _entry_point_type;
 159   }
 160   const llvm::FunctionType* osr_entry_point_type() const {
 161     return _osr_entry_point_type;
 162   }
 163 
 164   // Mappings
 165  private:
 166   const llvm::Type* _to_stackType[T_CONFLICT];
 167   const llvm::Type* _to_arrayType[T_CONFLICT];
 168 
 169  private:
 170   const llvm::Type* map_type(const llvm::Type* const* table,
 171                              BasicType                type) const {
 172     assert(type >= 0 && type < T_CONFLICT, "unhandled type");
 173     const llvm::Type* result = table[type];
 174     assert(result != NULL, "unhandled type");
 175     return result;
 176   }
 177 
 178  public:
 179   const llvm::Type* to_stackType(BasicType type) const {
 180     return map_type(_to_stackType, type);
 181   }
 182   const llvm::Type* to_arrayType(BasicType type) const {
 183     return map_type(_to_arrayType, type);
 184   }
 185 
 186   // Functions queued for freeing
 187  private:
 188   SharkFreeQueueItem* _free_queue;
 189 
 190  public:
 191   void push_to_free_queue(llvm::Function* function);
 192   llvm::Function* pop_from_free_queue();
 193 };
 194 
 195 #endif // SHARE_VM_SHARK_SHARKCONTEXT_HPP