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