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::never_null_at
 138 //
 139 // True if we statically know that the argument at 'index' is never null.
 140 bool ciSignature::is_never_null_at(int index) const {
 141   assert(index < _count, "out of bounds");
 142   return _types->at(index)->is_never_null();
 143 }
 144 
 145 // ------------------------------------------------------------------
 146 // ciSignature::equals
 147 //
 148 // Compare this signature to another one.  Signatures with different
 149 // accessing classes but with signature-types resolved to the same
 150 // types are defined to be equal.
 151 bool ciSignature::equals(ciSignature* that) {
 152   // Compare signature
 153   if (!this->as_symbol()->equals(that->as_symbol()))  return false;
 154   // Compare all types of the arguments
 155   for (int i = 0; i < _count; i++) {
 156     if (this->type_at(i) != that->type_at(i))         return false;
 157   }
 158   // Compare the return type
 159   if (this->return_type() != that->return_type())     return false;
 160   return true;
 161 }
 162 
 163 // ------------------------------------------------------------------
 164 // ciSignature::print_signature
 165 void ciSignature::print_signature() {
 166   _symbol->print_symbol();
 167 }
 168 
 169 // ------------------------------------------------------------------
 170 // ciSignature::print
 171 void ciSignature::print() {
 172   tty->print("<ciSignature symbol=");
 173   print_signature();
 174  tty->print(" accessing_klass=");
 175   _accessing_klass->print();
 176   tty->print(" address=" INTPTR_FORMAT ">", p2i((address)this));
 177 }