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