1 /*
  2  * Copyright (c) 1999, 2025, 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 "ci/ciConstant.hpp"
 26 #include "ci/ciField.hpp"
 27 #include "ci/ciInstance.hpp"
 28 #include "ci/ciInstanceKlass.hpp"
 29 #include "ci/ciNullObject.hpp"
 30 #include "ci/ciUtilities.inline.hpp"
 31 #include "classfile/javaClasses.inline.hpp"
 32 #include "classfile/vmClasses.hpp"
 33 #include "oops/oop.inline.hpp"
 34 
 35 // ciInstance
 36 //
 37 // This class represents an instanceOop in the HotSpot virtual
 38 // machine.
 39 
 40 // ------------------------------------------------------------------
 41 // ciObject::java_mirror_type
 42 ciType* ciInstance::java_mirror_type(bool* is_null_free_array) {
 43   VM_ENTRY_MARK;
 44   oop m = get_oop();
 45   // Return null if it is not java.lang.Class.
 46   if (m == nullptr || m->klass() != vmClasses::Class_klass()) {
 47     return nullptr;
 48   }
 49   // Return either a primitive type or a klass.
 50   if (java_lang_Class::is_primitive(m)) {
 51     return ciType::make(java_lang_Class::primitive_type(m));
 52   } else {
 53     Klass* k = java_lang_Class::as_Klass(m);
 54     assert(k != nullptr, "");
 55     if (is_null_free_array != nullptr && (k->is_array_klass() && k->is_null_free_array_klass())) {
 56       *is_null_free_array = true;
 57     }
 58     return CURRENT_THREAD_ENV->get_klass(k);
 59   }
 60 }
 61 
 62 // ------------------------------------------------------------------
 63 // ciInstance::field_value_impl
 64 ciConstant ciInstance::field_value_impl(BasicType field_btype, int offset) {
 65   ciConstant value = check_constant_value_cache(offset, field_btype);
 66   if (value.is_valid()) {
 67     return value;
 68   }
 69   VM_ENTRY_MARK;
 70   oop obj = get_oop();
 71   assert(obj != nullptr, "bad oop");
 72   switch(field_btype) {
 73     case T_BYTE:    value = ciConstant(field_btype, obj->byte_field(offset)); break;
 74     case T_CHAR:    value = ciConstant(field_btype, obj->char_field(offset)); break;
 75     case T_SHORT:   value = ciConstant(field_btype, obj->short_field(offset)); break;
 76     case T_BOOLEAN: value = ciConstant(field_btype, obj->bool_field(offset)); break;
 77     case T_INT:     value = ciConstant(field_btype, obj->int_field(offset)); break;
 78     case T_FLOAT:   value = ciConstant(obj->float_field(offset)); break;
 79     case T_DOUBLE:  value = ciConstant(obj->double_field(offset)); break;
 80     case T_LONG:    value = ciConstant(obj->long_field(offset)); break;
 81     case T_OBJECT:  // fall through
 82     case T_ARRAY: {
 83       oop o = obj->obj_field(offset);
 84 
 85       // A field will be "constant" if it is known always to be
 86       // a non-null reference to an instance of a particular class,
 87       // or to a particular array.  This can happen even if the instance
 88       // or array is not perm.  In such a case, an "unloaded" ciArray
 89       // or ciInstance is created.  The compiler may be able to use
 90       // information about the object's class (which is exact) or length.
 91 
 92       if (o == nullptr) {
 93         value = ciConstant(field_btype, ciNullObject::make());
 94       } else {
 95         value = ciConstant(field_btype, CURRENT_ENV->get_object(o));
 96       }
 97       break;
 98     }
 99     default:
100       fatal("no field value: %s", type2name(field_btype));
101   }
102   add_to_constant_value_cache(offset, value);
103   return value;
104 }
105 
106 // ------------------------------------------------------------------
107 // ciInstance::field_value
108 //
109 // Constant value of a field.
110 ciConstant ciInstance::field_value(ciField* field) {
111   assert(is_loaded(), "invalid access - must be loaded");
112   assert(field->holder()->is_loaded(), "invalid access - holder must be loaded");
113   assert(field->is_static() || field->holder()->is_inlinetype() || klass()->is_subclass_of(field->holder()),
114          "invalid access - must be subclass");
115 
116   return field_value_impl(field->type()->basic_type(), field->offset_in_bytes());
117 }
118 
119 // ------------------------------------------------------------------
120 // ciInstance::field_value_by_offset
121 //
122 // Constant value of a field at the specified offset.
123 ciConstant ciInstance::field_value_by_offset(int field_offset) {
124   ciInstanceKlass* ik = klass()->as_instance_klass();
125   ciField* field = ik->get_field_by_offset(field_offset, false);
126   if (field == nullptr)
127     return ciConstant();  // T_ILLEGAL
128   return field_value(field);
129 }
130 
131 // ------------------------------------------------------------------
132 // ciInstance::print_impl
133 //
134 // Implementation of the print method.
135 void ciInstance::print_impl(outputStream* st) {
136   st->print(" type=");
137   klass()->print(st);
138 }
139 
140 
141 ciKlass* ciInstance::java_lang_Class_klass() {
142   VM_ENTRY_MARK;
143   assert(java_lang_Class::as_Klass(get_oop()) != nullptr, "klass is null");
144   return CURRENT_ENV->get_metadata(java_lang_Class::as_Klass(get_oop()))->as_klass();
145 }