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