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