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