1 /*
   2  * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "ci/ciEnv.hpp"
  27 #include "ci/ciType.hpp"
  28 #include "ci/ciUtilities.inline.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/oop.inline.hpp"
  32 
  33 ciType* ciType::_basic_types[T_CONFLICT+1];
  34 
  35 // ciType
  36 //
  37 // This class represents either a class (T_OBJECT), array (T_ARRAY),
  38 // or one of the primitive types such as T_INT.
  39 
  40 // ------------------------------------------------------------------
  41 // ciType::ciType
  42 //
  43 ciType::ciType(BasicType basic_type) : ciMetadata() {
  44   assert(basic_type >= T_BOOLEAN && basic_type <= T_CONFLICT, "range check");
  45   _basic_type = basic_type;
  46 }
  47 
  48 ciType::ciType(Klass* k) : ciMetadata(k) {
  49   _basic_type = k->is_array_klass() ? T_ARRAY : T_OBJECT;
  50 }
  51 
  52 
  53 // ------------------------------------------------------------------
  54 // ciType::is_subtype_of
  55 //
  56 bool ciType::is_subtype_of(ciType* type) {
  57   if (this == type)  return true;
  58   if (is_klass() && type->is_klass())
  59     return this->as_klass()->is_subtype_of(type->as_klass());
  60   return false;
  61 }
  62 
  63 // ------------------------------------------------------------------
  64 // ciType::name
  65 //
  66 // Return the name of this type
  67 const char* ciType::name() {
  68   if (is_primitive_type()) {
  69     return type2name(basic_type());
  70   } else {
  71     assert(is_klass(), "must be");
  72     return as_klass()->name()->as_utf8();
  73   }
  74 }
  75 
  76 // ------------------------------------------------------------------
  77 // ciType::print_impl
  78 //
  79 // Implementation of the print method.
  80 void ciType::print_impl(outputStream* st) {
  81   st->print(" type=");
  82   print_name_on(st);
  83 }
  84 
  85 // ------------------------------------------------------------------
  86 // ciType::print_name
  87 //
  88 // Print the name of this type
  89 void ciType::print_name_on(outputStream* st) {
  90   ResourceMark rm;
  91   st->print("%s", name());
  92 }
  93 
  94 
  95 
  96 // ------------------------------------------------------------------
  97 // ciType::java_mirror
  98 //
  99 ciInstance* ciType::java_mirror() {
 100   VM_ENTRY_MARK;
 101   return CURRENT_THREAD_ENV->get_instance(Universe::java_mirror(basic_type()));
 102 }
 103 
 104 // ------------------------------------------------------------------
 105 // ciType::box_klass
 106 //
 107 ciKlass* ciType::box_klass() {
 108   if (!is_primitive_type())  return this->as_klass();  // reference types are "self boxing"
 109 
 110   // Void is "boxed" with a null.
 111   if (basic_type() == T_VOID)  return NULL;
 112 
 113   VM_ENTRY_MARK;
 114   return CURRENT_THREAD_ENV->get_instance_klass(SystemDictionary::box_klass(basic_type()));
 115 }
 116 
 117 
 118 // ------------------------------------------------------------------
 119 // ciType::make
 120 //
 121 // Produce the ciType for a given primitive BasicType.
 122 // As a bonus, produce the right reference type for T_OBJECT.
 123 // Does not work on T_ARRAY.
 124 ciType* ciType::make(BasicType t) {
 125   // short, etc.
 126   // Note: Bare T_ADDRESS means a raw pointer type, not a return_address.
 127   assert((uint)t < T_CONFLICT+1, "range check");
 128   if (t == T_OBJECT)  return ciEnv::_Object_klass;  // java/lang/Object
 129   assert(_basic_types[t] != NULL, "domain check");
 130   return _basic_types[t];
 131 }
 132 
 133 // ciReturnAddress
 134 //
 135 // This class represents the type of a specific return address in the
 136 // bytecodes.
 137 
 138 // ------------------------------------------------------------------
 139 // ciReturnAddress::ciReturnAddress
 140 //
 141 ciReturnAddress::ciReturnAddress(int bci) : ciType(T_ADDRESS) {
 142   assert(0 <= bci, "bci cannot be negative");
 143   _bci = bci;
 144 }
 145 
 146 // ------------------------------------------------------------------
 147 // ciReturnAddress::print_impl
 148 //
 149 // Implementation of the print method.
 150 void ciReturnAddress::print_impl(outputStream* st) {
 151   st->print(" bci=%d", _bci);
 152 }
 153 
 154 // ------------------------------------------------------------------
 155 // ciReturnAddress::make
 156 ciReturnAddress* ciReturnAddress::make(int bci) {
 157   GUARDED_VM_ENTRY(return CURRENT_ENV->get_return_address(bci);)
 158 }