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 #include "precompiled.hpp"
  27 #include "oops/arrayOop.hpp"
  28 #include "oops/oop.hpp"
  29 #include "shark/llvmHeaders.hpp"
  30 #include "shark/sharkContext.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 
  33 using namespace llvm;
  34 
  35 SharkContext::SharkContext(const char* name)
  36   : LLVMContext(),
  37     _free_queue(NULL) {
  38   // Create a module to build our functions into
  39   _module = new Module(name, *this);
  40 
  41   // Create basic types
  42   _void_type    = Type::getVoidTy(*this);
  43   _bit_type     = Type::getInt1Ty(*this);
  44   _jbyte_type   = Type::getInt8Ty(*this);
  45   _jshort_type  = Type::getInt16Ty(*this);
  46   _jint_type    = Type::getInt32Ty(*this);
  47   _jlong_type   = Type::getInt64Ty(*this);
  48   _jfloat_type  = Type::getFloatTy(*this);
  49   _jdouble_type = Type::getDoubleTy(*this);
  50 
  51   // Create compound types
  52   _itableOffsetEntry_type = PointerType::getUnqual(
  53     ArrayType::get(jbyte_type(), itableOffsetEntry::size() * wordSize));
  54 
  55   _klass_type = PointerType::getUnqual(
  56     ArrayType::get(jbyte_type(), sizeof(Klass)));
  57 
  58   _jniEnv_type = PointerType::getUnqual(
  59     ArrayType::get(jbyte_type(), sizeof(JNIEnv)));
  60 
  61   _jniHandleBlock_type = PointerType::getUnqual(
  62     ArrayType::get(jbyte_type(), sizeof(JNIHandleBlock)));
  63 
  64   _Method*_type = PointerType::getUnqual(
  65     ArrayType::get(jbyte_type(), sizeof(Method)));
  66 
  67   _monitor_type = ArrayType::get(
  68     jbyte_type(), frame::interpreter_frame_monitor_size() * wordSize);
  69 
  70   _oop_type = PointerType::getUnqual(
  71     ArrayType::get(jbyte_type(), sizeof(oopDesc)));
  72 
  73   _thread_type = PointerType::getUnqual(
  74     ArrayType::get(jbyte_type(), sizeof(JavaThread)));
  75 
  76   _zeroStack_type = PointerType::getUnqual(
  77     ArrayType::get(jbyte_type(), sizeof(ZeroStack)));
  78 
  79   std::vector<const Type*> params;
  80   params.push_back(Method*_type());
  81   params.push_back(intptr_type());
  82   params.push_back(thread_type());
  83   _entry_point_type = FunctionType::get(jint_type(), params, false);
  84 
  85   params.clear();
  86   params.push_back(Method*_type());
  87   params.push_back(PointerType::getUnqual(jbyte_type()));
  88   params.push_back(intptr_type());
  89   params.push_back(thread_type());
  90   _osr_entry_point_type = FunctionType::get(jint_type(), params, false);
  91 
  92   // Create mappings
  93   for (int i = 0; i < T_CONFLICT; i++) {
  94     switch (i) {
  95     case T_BOOLEAN:
  96       _to_stackType[i] = jint_type();
  97       _to_arrayType[i] = jbyte_type();
  98       break;
  99 
 100     case T_BYTE:
 101       _to_stackType[i] = jint_type();
 102       _to_arrayType[i] = jbyte_type();
 103       break;
 104 
 105     case T_CHAR:
 106       _to_stackType[i] = jint_type();
 107       _to_arrayType[i] = jshort_type();
 108       break;
 109 
 110     case T_SHORT:
 111       _to_stackType[i] = jint_type();
 112       _to_arrayType[i] = jshort_type();
 113       break;
 114 
 115     case T_INT:
 116       _to_stackType[i] = jint_type();
 117       _to_arrayType[i] = jint_type();
 118       break;
 119 
 120     case T_LONG:
 121       _to_stackType[i] = jlong_type();
 122       _to_arrayType[i] = jlong_type();
 123       break;
 124 
 125     case T_FLOAT:
 126       _to_stackType[i] = jfloat_type();
 127       _to_arrayType[i] = jfloat_type();
 128       break;
 129 
 130     case T_DOUBLE:
 131       _to_stackType[i] = jdouble_type();
 132       _to_arrayType[i] = jdouble_type();
 133       break;
 134 
 135     case T_OBJECT:
 136     case T_ARRAY:
 137       _to_stackType[i] = oop_type();
 138       _to_arrayType[i] = oop_type();
 139       break;
 140 
 141     case T_ADDRESS:
 142       _to_stackType[i] = intptr_type();
 143       _to_arrayType[i] = NULL;
 144       break;
 145 
 146     default:
 147       _to_stackType[i] = NULL;
 148       _to_arrayType[i] = NULL;
 149     }
 150   }
 151 }
 152 
 153 class SharkFreeQueueItem : public CHeapObj {
 154  public:
 155   SharkFreeQueueItem(llvm::Function* function, SharkFreeQueueItem *next)
 156     : _function(function), _next(next) {}
 157 
 158  private:
 159   llvm::Function*     _function;
 160   SharkFreeQueueItem* _next;
 161 
 162  public:
 163   llvm::Function* function() const {
 164     return _function;
 165   }
 166   SharkFreeQueueItem* next() const {
 167     return _next;
 168   }
 169 };
 170 
 171 void SharkContext::push_to_free_queue(Function* function) {
 172   _free_queue = new SharkFreeQueueItem(function, _free_queue);
 173 }
 174 
 175 Function* SharkContext::pop_from_free_queue() {
 176   if (_free_queue == NULL)
 177     return NULL;
 178 
 179   SharkFreeQueueItem *item = _free_queue;
 180   Function *function = item->function();
 181   _free_queue = item->next();
 182   delete item;
 183   return function;
 184 }