1 /*
   2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2008, 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_SHARKBUILDER_HPP
  27 #define SHARE_VM_SHARK_SHARKBUILDER_HPP
  28 
  29 #include "ci/ciType.hpp"
  30 #include "memory/barrierSet.hpp"
  31 #include "memory/cardTableModRefBS.hpp"
  32 #include "shark/llvmHeaders.hpp"
  33 #include "shark/llvmValue.hpp"
  34 #include "shark/sharkCodeBuffer.hpp"
  35 #include "shark/sharkEntry.hpp"
  36 #include "shark/sharkType.hpp"
  37 #include "shark/sharkValue.hpp"
  38 #include "utilities/debug.hpp"
  39 #include "utilities/sizes.hpp"
  40 
  41 class SharkBuilder : public llvm::IRBuilder<> {
  42   friend class SharkCompileInvariants;
  43 
  44  public:
  45   SharkBuilder(SharkCodeBuffer* code_buffer);
  46 
  47   // The code buffer we are building into.
  48  private:
  49   SharkCodeBuffer* _code_buffer;
  50 
  51  protected:
  52   SharkCodeBuffer* code_buffer() const {
  53     return _code_buffer;
  54   }
  55 
  56  public:
  57   llvm::LoadInst* CreateAtomicLoad(llvm::Value* ptr,
  58                                    unsigned align = HeapWordSize,
  59                                    llvm::AtomicOrdering ordering = llvm::SequentiallyConsistent,
  60                                    llvm::SynchronizationScope synchScope = llvm::CrossThread,
  61                                    bool isVolatile = true,
  62                                    const char *name = "");
  63   llvm::StoreInst* CreateAtomicStore(llvm::Value *val,
  64                                      llvm::Value *ptr,
  65                                      unsigned align = HeapWordSize,
  66                                      llvm::AtomicOrdering ordering = llvm::SequentiallyConsistent,
  67                                      llvm::SynchronizationScope SynchScope = llvm::CrossThread,
  68                                      bool isVolatile = true,
  69                                      const char *name = "");
  70 
  71   // Helpers for accessing structures.
  72  public:
  73   llvm::Value* CreateAddressOfStructEntry(llvm::Value* base,
  74                                           ByteSize offset,
  75                                           llvm::Type* type,
  76                                           const char *name = "");
  77   llvm::LoadInst* CreateValueOfStructEntry(llvm::Value* base,
  78                                            ByteSize offset,
  79                                            llvm::Type* type,
  80                                            const char *name = "");
  81 
  82   // Helpers for accessing arrays.
  83  public:
  84   llvm::LoadInst* CreateArrayLength(llvm::Value* arrayoop);
  85   llvm::Value* CreateArrayAddress(llvm::Value*      arrayoop,
  86                                   llvm::Type* element_type,
  87                                   int               element_bytes,
  88                                   ByteSize          base_offset,
  89                                   llvm::Value*      index,
  90                                   const char*       name = "");
  91   llvm::Value* CreateArrayAddress(llvm::Value* arrayoop,
  92                                   BasicType    basic_type,
  93                                   ByteSize     base_offset,
  94                                   llvm::Value* index,
  95                                   const char*  name = "");
  96   llvm::Value* CreateArrayAddress(llvm::Value* arrayoop,
  97                                   BasicType    basic_type,
  98                                   llvm::Value* index,
  99                                   const char*  name = "");
 100 
 101   // Helpers for creating intrinsics and external functions.
 102  private:
 103   static llvm::Type* make_type(char type, bool void_ok);
 104   static llvm::FunctionType* make_ftype(const char* params,
 105                                               const char* ret);
 106   llvm::Value* make_function(const char* name,
 107                              const char* params,
 108                              const char* ret);
 109   llvm::Value* make_function(address     func,
 110                              const char* params,
 111                              const char* ret);
 112 
 113   // Intrinsics and external functions, part 1: VM calls.
 114   //   These are functions declared with JRT_ENTRY and JRT_EXIT,
 115   //   macros which flip the thread from _thread_in_Java to
 116   //   _thread_in_vm and back.  VM calls always safepoint, and can
 117   //   therefore throw exceptions.  VM calls require of setup and
 118   //   teardown, and must be called with SharkTopLevelBlock::call_vm.
 119  public:
 120   llvm::Value* find_exception_handler();
 121   llvm::Value* monitorenter();
 122   llvm::Value* monitorexit();
 123   llvm::Value* new_instance();
 124   llvm::Value* newarray();
 125   llvm::Value* anewarray();
 126   llvm::Value* multianewarray();
 127   llvm::Value* register_finalizer();
 128   llvm::Value* safepoint();
 129   llvm::Value* throw_ArithmeticException();
 130   llvm::Value* throw_ArrayIndexOutOfBoundsException();
 131   llvm::Value* throw_ClassCastException();
 132   llvm::Value* throw_NullPointerException();
 133 
 134   // Intrinsics and external functions, part 2: High-level non-VM calls.
 135   //   These are called like normal functions.  The stack is not set
 136   //   up for walking so they must not safepoint or throw exceptions,
 137   //   or call anything that might.
 138  public:
 139   llvm::Value* f2i();
 140   llvm::Value* f2l();
 141   llvm::Value* d2i();
 142   llvm::Value* d2l();
 143   llvm::Value* is_subtype_of();
 144   llvm::Value* current_time_millis();
 145   llvm::Value* sin();
 146   llvm::Value* cos();
 147   llvm::Value* tan();
 148   llvm::Value* atan2();
 149   llvm::Value* sqrt();
 150   llvm::Value* log();
 151   llvm::Value* log10();
 152   llvm::Value* pow();
 153   llvm::Value* exp();
 154   llvm::Value* fabs();
 155   llvm::Value* unsafe_field_offset_to_byte_offset();
 156   llvm::Value* osr_migration_end();
 157 
 158   // Intrinsics and external functions, part 3: semi-VM calls.
 159   //   These are special cases that do VM call stuff but are invoked
 160   //   as though they were normal calls.  This is acceptable so long
 161   //   as the method that calls them returns to its immediately that
 162   //   the semi VM call returns.
 163  public:
 164   llvm::Value* throw_StackOverflowError();
 165   llvm::Value* uncommon_trap();
 166   llvm::Value* deoptimized_entry_point();
 167 
 168   // Intrinsics and external functions, part 4: Native-Java transition.
 169   //   This is a special case in that it is invoked during a thread
 170   //   state transition.  The stack must be set up for walking, and it
 171   //   may throw exceptions, but the state is _thread_in_native_trans.
 172  public:
 173   llvm::Value* check_special_condition_for_native_trans();
 174 
 175   // Intrinsics and external functions, part 5: Low-level non-VM calls.
 176   //   These have the same caveats as the high-level non-VM calls
 177   //   above.  They are not accessed directly; rather, you should
 178   //   access them via the various Create* methods below.
 179  private:
 180   llvm::Value* cmpxchg_int();
 181   llvm::Value* cmpxchg_ptr();
 182   llvm::Value* frame_address();
 183   llvm::Value* memset();
 184   llvm::Value* unimplemented();
 185   llvm::Value* should_not_reach_here();
 186   llvm::Value* dump();
 187 
 188   // Public interface to low-level non-VM calls.
 189  public:
 190   llvm::CallInst* CreateGetFrameAddress();
 191   llvm::CallInst* CreateMemset(llvm::Value* dst,
 192                                llvm::Value* value,
 193                                llvm::Value* len,
 194                                llvm::Value* align);
 195   llvm::CallInst* CreateUnimplemented(const char* file, int line);
 196   llvm::CallInst* CreateShouldNotReachHere(const char* file, int line);
 197   NOT_PRODUCT(llvm::CallInst* CreateDump(llvm::Value* value));
 198 
 199   // HotSpot memory barriers
 200  public:
 201   void CreateUpdateBarrierSet(BarrierSet* bs, llvm::Value* field);
 202 
 203   // Helpers for accessing the code buffer.
 204  public:
 205   llvm::Value* code_buffer_address(int offset);
 206   llvm::Value* CreateInlineOop(jobject object, const char* name = "");
 207   llvm::Value* CreateInlineOop(ciObject* object, const char* name = "") {
 208     return CreateInlineOop(object->constant_encoding(), name);
 209   }
 210 
 211   llvm::Value* CreateInlineMetadata(Metadata* metadata, llvm::PointerType* type, const char* name = "");
 212   llvm::Value* CreateInlineMetadata(ciMetadata* metadata, llvm::PointerType* type, const char* name = "") {
 213     return CreateInlineMetadata(metadata->constant_encoding(), type, name);
 214   }
 215   llvm::Value* CreateInlineData(void*             data,
 216                                 size_t            size,
 217                                 llvm::Type* type,
 218                                 const char*       name = "");
 219 
 220   // Helpers for creating basic blocks.
 221   // NB don't use unless SharkFunction::CreateBlock is unavailable.
 222   // XXX these are hacky and should be removed.
 223  public:
 224   llvm::BasicBlock* GetBlockInsertionPoint() const;
 225   llvm::BasicBlock* CreateBlock(llvm::BasicBlock* ip,
 226                                 const char*       name="") const;
 227 };
 228   #endif // SHARE_VM_SHARK_SHARKBUILDER_HPP