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