1 /*
   2  * Copyright (c) 1999, 2019, 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/ciMethodType.hpp"
  27 #include "ci/ciSignature.hpp"
  28 #include "ci/ciUtilities.inline.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "runtime/signature.hpp"
  33 
  34 // ciSignature
  35 //
  36 // This class represents the signature of a method.
  37 
  38 // ------------------------------------------------------------------
  39 // ciSignature::ciSignature
  40 ciSignature::ciSignature(ciKlass* accessing_klass, const constantPoolHandle& cpool, ciSymbol* symbol) {
  41   ASSERT_IN_VM;
  42   EXCEPTION_CONTEXT;
  43   _accessing_klass = accessing_klass;
  44   _symbol = symbol;
  45 
  46   ciEnv* env = CURRENT_ENV;
  47   Arena* arena = env->arena();
  48   _types = new (arena) GrowableArray<ciType*>(arena, 8, 0, NULL);
  49 
  50   int size = 0;
  51   int count = 0;
  52   ResourceMark rm(THREAD);
  53   Symbol* sh = symbol->get_symbol();
  54   SignatureStream ss(sh);
  55   for (; ; ss.next()) {
  56     // Process one element of the signature
  57     ciType* type;
  58     if (!ss.is_object()) {
  59       type = ciType::make(ss.type());
  60     } else {
  61       Symbol* name = ss.as_symbol();
  62       ciSymbol* klass_name = env->get_symbol(name);
  63       type = env->get_klass_by_name_impl(_accessing_klass, cpool, klass_name, false);
  64     }
  65     if (type->is_valuetype() && ss.type() == T_VALUETYPE) {
  66       type = env->make_never_null_wrapper(type);
  67     }
  68     _types->append(type);
  69     if (ss.at_return_type()) {
  70       // Done processing the return type; do not add it into the count.
  71       break;
  72     }
  73     size += type->size();
  74     count++;
  75   }
  76   _size = size;
  77   _count = count;
  78 }
  79 
  80 // ------------------------------------------------------------------
  81 // ciSignature::ciSignature
  82 ciSignature::ciSignature(ciKlass* accessing_klass, ciSymbol* symbol, ciMethodType* method_type) :
  83   _symbol(symbol),
  84   _accessing_klass(accessing_klass),
  85   _size( method_type->ptype_slot_count()),
  86   _count(method_type->ptype_count())
  87 {
  88   ASSERT_IN_VM;
  89   EXCEPTION_CONTEXT;
  90   ciEnv* env =  CURRENT_ENV;
  91   Arena* arena = env->arena();
  92   _types = new (arena) GrowableArray<ciType*>(arena, _count + 1, 0, NULL);
  93   ciType* type = NULL;
  94   bool never_null = false;
  95   for (int i = 0; i < _count; i++) {
  96     type = method_type->ptype_at(i, never_null);
  97     if (type->is_valuetype() && never_null) {
  98       type = env->make_never_null_wrapper(type);
  99     }
 100     _types->append(type);
 101   }
 102   type = method_type->rtype(never_null);
 103   if (type->is_valuetype() && never_null) {
 104     type = env->make_never_null_wrapper(type);
 105   }
 106   _types->append(type);
 107 }
 108 
 109 // ------------------------------------------------------------------
 110 // ciSignature::return_type
 111 //
 112 // What is the return type of this signature?
 113 ciType* ciSignature::return_type() const {
 114   return _types->at(_count)->unwrap();
 115 }
 116 
 117 // ------------------------------------------------------------------
 118 // ciSignature::type_at
 119 //
 120 // What is the type of the index'th element of this
 121 // signature?
 122 ciType* ciSignature::type_at(int index) const {
 123   assert(index < _count, "out of bounds");
 124   // The first _klasses element holds the return klass.
 125   return _types->at(index)->unwrap();
 126 }
 127 
 128 // ------------------------------------------------------------------
 129 // ciSignature::return_never_null
 130 //
 131 // True if we statically know that the return value is never null.
 132 bool ciSignature::returns_never_null() const {
 133   return _types->at(_count)->is_never_null();
 134 }
 135 
 136 // ------------------------------------------------------------------
 137 // ciSignature::return_never_null
 138 //
 139 // True if we statically know that the return value is never null, or
 140 // if the return type has a Q signature but is not yet loaded, in which case
 141 // it could be a never-null type.
 142 bool ciSignature::maybe_returns_never_null() const {
 143   ciType* ret_type = _types->at(_count);
 144   if (ret_type->is_never_null()) {
 145     return true;
 146   } else if (ret_type->is_instance_klass() && !ret_type->as_instance_klass()->is_loaded()) {
 147     GUARDED_VM_ENTRY(if (get_symbol()->is_Q_method_signature()) { return true; })
 148   }
 149   return false;
 150 }
 151 
 152 // ------------------------------------------------------------------
 153 // ciSignature::never_null_at
 154 //
 155 // True if we statically know that the argument at 'index' is never null.
 156 bool ciSignature::is_never_null_at(int index) const {
 157   assert(index < _count, "out of bounds");
 158   return _types->at(index)->is_never_null();
 159 }
 160 
 161 // ------------------------------------------------------------------
 162 // ciSignature::equals
 163 //
 164 // Compare this signature to another one.  Signatures with different
 165 // accessing classes but with signature-types resolved to the same
 166 // types are defined to be equal.
 167 bool ciSignature::equals(ciSignature* that) {
 168   // Compare signature
 169   if (!this->as_symbol()->equals(that->as_symbol()))  return false;
 170   // Compare all types of the arguments
 171   for (int i = 0; i < _count; i++) {
 172     if (this->type_at(i) != that->type_at(i))         return false;
 173   }
 174   // Compare the return type
 175   if (this->return_type() != that->return_type())     return false;
 176   return true;
 177 }
 178 
 179 // ------------------------------------------------------------------
 180 // ciSignature::print_signature
 181 void ciSignature::print_signature() {
 182   _symbol->print_symbol();
 183 }
 184 
 185 // ------------------------------------------------------------------
 186 // ciSignature::print
 187 void ciSignature::print() {
 188   tty->print("<ciSignature symbol=");
 189   print_signature();
 190  tty->print(" accessing_klass=");
 191   _accessing_klass->print();
 192   tty->print(" address=" INTPTR_FORMAT ">", p2i((address)this));
 193 }