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