< prev index next >

src/hotspot/share/ci/ciInstanceKlass.cpp

Print this page

  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/ciField.hpp"

 26 #include "ci/ciInstance.hpp"
 27 #include "ci/ciInstanceKlass.hpp"
 28 #include "ci/ciUtilities.inline.hpp"
 29 #include "classfile/javaClasses.hpp"

 30 #include "classfile/vmClasses.hpp"
 31 #include "memory/allocation.hpp"
 32 #include "memory/allocation.inline.hpp"
 33 #include "memory/resourceArea.hpp"
 34 #include "oops/fieldStreams.inline.hpp"
 35 #include "oops/instanceKlass.inline.hpp"
 36 #include "oops/klass.inline.hpp"
 37 #include "oops/oop.inline.hpp"

 38 #include "runtime/fieldDescriptor.inline.hpp"
 39 #include "runtime/handles.inline.hpp"
 40 #include "runtime/jniHandles.inline.hpp"
 41 
 42 // ciInstanceKlass
 43 //
 44 // This class represents a Klass* in the HotSpot virtual machine
 45 // whose Klass part in an InstanceKlass.
 46 
 47 
 48 // ------------------------------------------------------------------
 49 // ciInstanceKlass::ciInstanceKlass
 50 //
 51 // Loaded instance klass.
 52 ciInstanceKlass::ciInstanceKlass(Klass* k) :
 53   ciKlass(k)
 54 {
 55   assert(get_Klass()->is_instance_klass(), "wrong type");
 56   assert(get_instanceKlass()->is_loaded(), "must be at least loaded");
 57   InstanceKlass* ik = get_instanceKlass();
 58 
 59   AccessFlags access_flags = ik->access_flags();
 60   _flags = ciFlags(access_flags);
 61   _has_finalizer = ik->has_finalizer();
 62   _has_subklass = flags().is_final() ? subklass_false : subklass_unknown;
 63   _init_state = ik->init_state();
 64   _has_nonstatic_fields = ik->has_nonstatic_fields();
 65   _has_nonstatic_concrete_methods = ik->has_nonstatic_concrete_methods();
 66   _is_hidden = ik->is_hidden();
 67   _is_record = ik->is_record();


 68   _trust_final_fields = ik->trust_final_fields();
 69   _nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields:
 70   _has_injected_fields = -1;
 71   _implementor = nullptr; // we will fill these lazily
 72   _transitive_interfaces = nullptr;
 73 
 74   // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC.
 75   // This is primarily useful for metadata which is considered as weak roots
 76   // by the GC but need to be strong roots if reachable from a current compilation.
 77   // InstanceKlass are created for both weak and strong metadata.  Ensuring this metadata
 78   // alive covers the cases where there are weak roots without performance cost.
 79   oop holder = ik->klass_holder();
 80   if (ik->class_loader_data()->has_class_mirror_holder()) {
 81     // Though ciInstanceKlass records class loader oop, it's not enough to keep
 82     // non-strong hidden classes alive (loader == nullptr). Klass holder should
 83     // be used instead. It is enough to record a ciObject, since cached elements are never removed
 84     // during ciObjectFactory lifetime. ciObjectFactory itself is created for
 85     // every compilation and lives for the whole duration of the compilation.
 86     assert(holder != nullptr, "holder of hidden class is the mirror which is never null");
 87     (void)CURRENT_ENV->get_object(holder);
 88   }
 89 
 90   JavaThread *thread = JavaThread::current();
 91   if (ciObjectFactory::is_initialized()) {

 98   }
 99 
100   _has_trusted_loader = compute_has_trusted_loader();
101 
102   // Lazy fields get filled in only upon request.
103   _super  = nullptr;
104   _java_mirror = nullptr;
105 
106   if (is_shared()) {
107     if (k != vmClasses::Object_klass()) {
108       super();
109     }
110     //compute_nonstatic_fields();  // done outside of constructor
111   }
112 
113   _field_cache = nullptr;
114 }
115 
116 // Version for unloaded classes:
117 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
118                                  jobject loader)
119   : ciKlass(name, T_OBJECT)

120 {
121   assert(name->char_at(0) != JVM_SIGNATURE_ARRAY, "not an instance klass");
122   _init_state = (InstanceKlass::ClassState)0;
123   _has_nonstatic_fields = false;
124   _nonstatic_fields = nullptr;

125   _has_injected_fields = -1;
126   _is_hidden = false;
127   _is_record = false;
128   _loader = loader;
129   _is_shared = false;
130   _super = nullptr;
131   _java_mirror = nullptr;
132   _field_cache = nullptr;
133   _has_trusted_loader = compute_has_trusted_loader();
134 }
135 
136 
137 
138 // ------------------------------------------------------------------
139 // ciInstanceKlass::compute_shared_is_initialized
140 void ciInstanceKlass::compute_shared_init_state() {
141   GUARDED_VM_ENTRY(
142     InstanceKlass* ik = get_instanceKlass();
143     _init_state = ik->init_state();
144   )

302   if (name()->index_of_at(len+1, "/", 1) >= 0)
303     return false;
304 
305   return true;
306 }
307 
308 // ------------------------------------------------------------------
309 // ciInstanceKlass::print_impl
310 //
311 // Implementation of the print method.
312 void ciInstanceKlass::print_impl(outputStream* st) {
313   ciKlass::print_impl(st);
314   GUARDED_VM_ENTRY(st->print(" loader=" INTPTR_FORMAT, p2i(loader()));)
315   if (is_loaded()) {
316     st->print(" initialized=%s finalized=%s subklass=%s size=%d flags=",
317               bool_to_str(is_initialized()),
318               bool_to_str(has_finalizer()),
319               bool_to_str(has_subklass()),
320               layout_helper());
321 
322     _flags.print_klass_flags();
323 
324     if (_super) {
325       st->print(" super=");
326       _super->print_name();
327     }
328     if (_java_mirror) {
329       st->print(" mirror=PRESENT");
330     }
331   }
332 }
333 
334 // ------------------------------------------------------------------
335 // ciInstanceKlass::super
336 //
337 // Get the superklass of this klass.
338 ciInstanceKlass* ciInstanceKlass::super() {
339   assert(is_loaded(), "must be loaded");
340   if (_super == nullptr && !is_java_lang_Object()) {
341     GUARDED_VM_ENTRY(
342       Klass* super_klass = get_instanceKlass()->super();
343       _super = CURRENT_ENV->get_instance_klass(super_klass);
344     )
345   }
346   return _super;

368   if (!is_abstract())   return nullptr; // Only applies to abstract classes.
369   if (!has_subklass())  return nullptr; // Must have at least one subklass.
370   VM_ENTRY_MARK;
371   InstanceKlass* ik = get_instanceKlass();
372   Klass* up = ik->up_cast_abstract();
373   assert(up->is_instance_klass(), "must be InstanceKlass");
374   if (ik == up) {
375     return nullptr;
376   }
377   return CURRENT_THREAD_ENV->get_instance_klass(up);
378 }
379 
380 // ------------------------------------------------------------------
381 // ciInstanceKlass::has_finalizable_subclass
382 bool ciInstanceKlass::has_finalizable_subclass() {
383   if (!is_loaded())     return true;
384   VM_ENTRY_MARK;
385   return Dependencies::find_finalizable_subclass(get_instanceKlass()) != nullptr;
386 }
387 
388 // ------------------------------------------------------------------
389 // ciInstanceKlass::contains_field_offset
390 bool ciInstanceKlass::contains_field_offset(int offset) {
391   VM_ENTRY_MARK;
392   return get_instanceKlass()->contains_field_offset(offset);
393 }
394 
395 ciField* ciInstanceKlass::get_nonstatic_field_by_offset(const int field_offset) {
396   for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
397     ciField* field = _nonstatic_fields->at(i);
398     int field_off = field->offset_in_bytes();
399     if (field_off == field_offset)
400       return field;

401   }
402   return nullptr;
403 }
404 
405 // ------------------------------------------------------------------
406 // ciInstanceKlass::get_field_by_offset
407 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
408   if (!is_static) {
409     return get_nonstatic_field_by_offset(field_offset);
410   }

411   VM_ENTRY_MARK;
412   InstanceKlass* k = get_instanceKlass();
413   fieldDescriptor fd;
414   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
415     return nullptr;
416   }
417   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
418   return field;
419 }
420 

































421 // ------------------------------------------------------------------
422 // ciInstanceKlass::get_field_by_name
423 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
424   VM_ENTRY_MARK;
425   InstanceKlass* k = get_instanceKlass();
426   fieldDescriptor fd;
427   Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
428   if (def == nullptr) {
429     return nullptr;
430   }
431   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
432   return field;
433 }
434 


435 // This is essentially a shortcut for:
436 //   get_field_by_offset(field_offset, is_static)->layout_type()
437 // except this does not require allocating memory for a new ciField
438 BasicType ciInstanceKlass::get_field_type_by_offset(const int field_offset, const bool is_static) {
439   if (!is_static) {
440     ciField* field = get_nonstatic_field_by_offset(field_offset);
441     return field != nullptr ? field->layout_type() : T_ILLEGAL;
442   }
443 
444   // Avoid allocating a new ciField by obtaining the field type directly
445   VM_ENTRY_MARK;
446   InstanceKlass* k = get_instanceKlass();
447   fieldDescriptor fd;
448   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
449     return T_ILLEGAL;
450   }
451 
452   // Reproduce the behavior of ciField::layout_type
453   BasicType field_type = fd.field_type();
454   if (is_reference_type(field_type)) {
455     return T_OBJECT;
456   }
457   return type2field[make(field_type)->basic_type()];
458 }
459 
460 // ------------------------------------------------------------------
461 // ciInstanceKlass::compute_nonstatic_fields
462 int ciInstanceKlass::compute_nonstatic_fields() {
463   assert(is_loaded(), "must be loaded");
464 
465   if (_nonstatic_fields != nullptr)
466     return _nonstatic_fields->length();


467 
468   if (!has_nonstatic_fields()) {
469     Arena* arena = CURRENT_ENV->arena();
470     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, nullptr);
471     return 0;
472   }
473   assert(!is_java_lang_Object(), "bootstrap OK");
474 
475   ciInstanceKlass* super = this->super();
476   GrowableArray<ciField*>* super_fields = nullptr;
477   if (super != nullptr && super->has_nonstatic_fields()) {
478     int super_flen   = super->nof_nonstatic_fields();
479     super_fields = super->_nonstatic_fields;
480     assert(super_flen == 0 || super_fields != nullptr, "first get nof_fields");
481   }
482 
483   GrowableArray<ciField*>* fields = nullptr;
484   GUARDED_VM_ENTRY({
485       fields = compute_nonstatic_fields_impl(super_fields);
486     });
487 
488   if (fields == nullptr) {
489     // This can happen if this class (java.lang.Class) has invisible fields.
490     if (super_fields != nullptr) {
491       _nonstatic_fields = super_fields;
492       return super_fields->length();
493     } else {
494       return 0;
495     }
496   }
497 
498   int flen = fields->length();
499 
500   _nonstatic_fields = fields;
501   return flen;
502 }
503 
504 GrowableArray<ciField*>*
505 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
506                                                super_fields) {
507   ASSERT_IN_VM;
508   Arena* arena = CURRENT_ENV->arena();
509   int flen = 0;
510   GrowableArray<ciField*>* fields = nullptr;
511   InstanceKlass* k = get_instanceKlass();
512   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
513     if (fs.access_flags().is_static())  continue;
514     flen += 1;
515   }
516 
517   // allocate the array:
518   if (flen == 0) {
519     return nullptr;  // return nothing if none are locally declared
















520   }
521   if (super_fields != nullptr) {
522     flen += super_fields->length();



523   }
524   fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, nullptr);
525   if (super_fields != nullptr) {
526     fields->appendAll(super_fields);


527   }
528 
529   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
530     if (fs.access_flags().is_static())  continue;







531     fieldDescriptor& fd = fs.field_descriptor();
532     ciField* field = new (arena) ciField(&fd);
533     fields->append(field);




































534   }
535   assert(fields->length() == flen, "sanity");
536   return fields;
537 }
538 
539 bool ciInstanceKlass::compute_injected_fields_helper() {
540   ASSERT_IN_VM;
541   InstanceKlass* k = get_instanceKlass();
542 
543   for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
544     if (fs.access_flags().is_static())  continue;
545     return true;
546   }
547   return false;
548 }
549 
550 void ciInstanceKlass::compute_injected_fields() {
551   assert(is_loaded(), "must be loaded");
552 
553   int has_injected_fields = 0;
554   if (super() != nullptr && super()->has_injected_fields()) {
555     has_injected_fields = 1;
556   } else {

628     } else {
629       // Go into the VM to fetch the implementor.
630       VM_ENTRY_MARK;
631       InstanceKlass* ik = get_instanceKlass();
632       Klass* implk = ik->implementor();
633       if (implk != nullptr) {
634         if (implk == ik) {
635           // More than one implementors. Use 'this' in this case.
636           impl = this;
637         } else {
638           impl = CURRENT_THREAD_ENV->get_instance_klass(implk);
639         }
640       }
641     }
642     // Memoize this result.
643     _implementor = impl;
644   }
645   return impl;
646 }
647 

















648 // Utility class for printing of the contents of the static fields for
649 // use by compilation replay.  It only prints out the information that
650 // could be consumed by the compiler, so for primitive types it prints
651 // out the actual value.  For Strings it's the actual string value.
652 // For array types it it's first level array size since that's the
653 // only value which statically unchangeable.  For all other reference
654 // types it simply prints out the dynamic type.
655 
656 class StaticFinalFieldPrinter : public FieldClosure {

657   outputStream* _out;








658   const char*   _holder;
659  public:
660   StaticFinalFieldPrinter(outputStream* out, const char* holder) :
661     _out(out),
662     _holder(holder) {
663   }
664   void do_field(fieldDescriptor* fd) {
665     if (fd->is_final() && !fd->has_initial_value()) {
666       ResourceMark rm;
667       oop mirror = fd->field_holder()->java_mirror();
668       _out->print("staticfield %s %s %s ", _holder, fd->name()->as_quoted_ascii(), fd->signature()->as_quoted_ascii());
669       BasicType field_type = fd->field_type();
670       switch (field_type) {
671         case T_BYTE:    _out->print_cr("%d", mirror->byte_field(fd->offset()));   break;
672         case T_BOOLEAN: _out->print_cr("%d", mirror->bool_field(fd->offset()));   break;
673         case T_SHORT:   _out->print_cr("%d", mirror->short_field(fd->offset()));  break;
674         case T_CHAR:    _out->print_cr("%d", mirror->char_field(fd->offset()));   break;
675         case T_INT:     _out->print_cr("%d", mirror->int_field(fd->offset()));    break;
676         case T_LONG:    _out->print_cr(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset())));   break;
677         case T_FLOAT: {
678           float f = mirror->float_field(fd->offset());
679           _out->print_cr("%d", *(int*)&f);
680           break;
681         }
682         case T_DOUBLE: {
683           double d = mirror->double_field(fd->offset());
684           _out->print_cr(INT64_FORMAT, *(int64_t*)&d);
685           break;
686         }
687         case T_ARRAY:  // fall-through
688         case T_OBJECT: {
689           oop value =  mirror->obj_field_acquire(fd->offset());
690           if (value == nullptr) {
691             if (field_type == T_ARRAY) {
692               _out->print("%d", -1);
693             }
694             _out->cr();
695           } else if (value->is_instance()) {
696             assert(field_type == T_OBJECT, "");
697             if (value->is_a(vmClasses::String_klass())) {
698               const char* ascii_value = java_lang_String::as_quoted_ascii(value);
699               _out->print_cr("\"%s\"", (ascii_value != nullptr) ? ascii_value : "");
700             } else {
701               const char* klass_name  = value->klass()->name()->as_quoted_ascii();
702               _out->print_cr("%s", klass_name);
703             }
704           } else if (value->is_array()) {
705             typeArrayOop ta = (typeArrayOop)value;
706             _out->print("%d", ta->length());
707             if (value->is_objArray()) {
708               objArrayOop oa = (objArrayOop)value;
709               const char* klass_name  = value->klass()->name()->as_quoted_ascii();
710               _out->print(" %s", klass_name);
711             }
712             _out->cr();













713           } else {
714             ShouldNotReachHere();

715           }
716           break;
717         }
718         default:






719           ShouldNotReachHere();
720         }
721     }

























722   }
723 };
724 
725 const char *ciInstanceKlass::replay_name() const {
726   return CURRENT_ENV->replay_name(get_instanceKlass());
727 }
728 
729 void ciInstanceKlass::dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik) {
730   if (ik->is_hidden()) {
731     const char *name = CURRENT_ENV->dyno_name(ik);
732     if (name != nullptr) {
733       out->print_cr("instanceKlass %s # %s", name, ik->name()->as_quoted_ascii());
734     } else {
735       out->print_cr("# instanceKlass %s", ik->name()->as_quoted_ascii());
736     }
737   } else {
738     out->print_cr("instanceKlass %s", ik->name()->as_quoted_ascii());
739   }
740 }
741 
742 GrowableArray<ciInstanceKlass*>* ciInstanceKlass::transitive_interfaces() const{
743   if (_transitive_interfaces == nullptr) {

  1 /*
  2  * Copyright (c) 1999, 2026, 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/ciField.hpp"
 26 #include "ci/ciInlineKlass.hpp"
 27 #include "ci/ciInstance.hpp"
 28 #include "ci/ciInstanceKlass.hpp"
 29 #include "ci/ciUtilities.inline.hpp"
 30 #include "classfile/javaClasses.hpp"
 31 #include "classfile/systemDictionary.hpp"
 32 #include "classfile/vmClasses.hpp"
 33 #include "memory/allocation.hpp"
 34 #include "memory/allocation.inline.hpp"
 35 #include "memory/resourceArea.hpp"
 36 #include "oops/fieldStreams.inline.hpp"
 37 #include "oops/instanceKlass.inline.hpp"
 38 #include "oops/klass.inline.hpp"
 39 #include "oops/oop.inline.hpp"
 40 #include "runtime/arguments.hpp"
 41 #include "runtime/fieldDescriptor.inline.hpp"
 42 #include "runtime/handles.inline.hpp"
 43 #include "runtime/jniHandles.inline.hpp"
 44 
 45 // ciInstanceKlass
 46 //
 47 // This class represents a Klass* in the HotSpot virtual machine
 48 // whose Klass part in an InstanceKlass.
 49 
 50 
 51 // ------------------------------------------------------------------
 52 // ciInstanceKlass::ciInstanceKlass
 53 //
 54 // Loaded instance klass.
 55 ciInstanceKlass::ciInstanceKlass(Klass* k) :
 56   ciKlass(k)
 57 {
 58   assert(get_Klass()->is_instance_klass(), "wrong type");
 59   assert(get_instanceKlass()->is_loaded(), "must be at least loaded");
 60   InstanceKlass* ik = get_instanceKlass();
 61 
 62   AccessFlags access_flags = ik->access_flags();
 63   _flags = ciFlags(access_flags);
 64   _has_finalizer = ik->has_finalizer();
 65   _has_subklass = flags().is_final() ? subklass_false : subklass_unknown;
 66   _init_state = ik->init_state();
 67   _has_nonstatic_fields = ik->has_nonstatic_fields();
 68   _has_nonstatic_concrete_methods = ik->has_nonstatic_concrete_methods();
 69   _is_hidden = ik->is_hidden();
 70   _is_record = ik->is_record();
 71   _declared_nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields
 72   _nonstatic_fields = nullptr;          // initialized lazily by compute_nonstatic_fields
 73   _trust_final_fields = ik->trust_final_fields();

 74   _has_injected_fields = -1;
 75   _implementor = nullptr;               // we will fill these lazily
 76   _transitive_interfaces = nullptr;
 77 
 78   // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC.
 79   // This is primarily useful for metadata which is considered as weak roots
 80   // by the GC but need to be strong roots if reachable from a current compilation.
 81   // InstanceKlass are created for both weak and strong metadata.  Ensuring this metadata
 82   // alive covers the cases where there are weak roots without performance cost.
 83   oop holder = ik->klass_holder();
 84   if (ik->class_loader_data()->has_class_mirror_holder()) {
 85     // Though ciInstanceKlass records class loader oop, it's not enough to keep
 86     // non-strong hidden classes alive (loader == nullptr). Klass holder should
 87     // be used instead. It is enough to record a ciObject, since cached elements are never removed
 88     // during ciObjectFactory lifetime. ciObjectFactory itself is created for
 89     // every compilation and lives for the whole duration of the compilation.
 90     assert(holder != nullptr, "holder of hidden class is the mirror which is never null");
 91     (void)CURRENT_ENV->get_object(holder);
 92   }
 93 
 94   JavaThread *thread = JavaThread::current();
 95   if (ciObjectFactory::is_initialized()) {

102   }
103 
104   _has_trusted_loader = compute_has_trusted_loader();
105 
106   // Lazy fields get filled in only upon request.
107   _super  = nullptr;
108   _java_mirror = nullptr;
109 
110   if (is_shared()) {
111     if (k != vmClasses::Object_klass()) {
112       super();
113     }
114     //compute_nonstatic_fields();  // done outside of constructor
115   }
116 
117   _field_cache = nullptr;
118 }
119 
120 // Version for unloaded classes:
121 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
122                                  jobject loader,
123                                  BasicType bt)
124   : ciKlass(name, bt)
125 {
126   assert(name->char_at(0) != JVM_SIGNATURE_ARRAY, "not an instance klass");
127   _init_state = (InstanceKlass::ClassState)0;
128   _has_nonstatic_fields = false;
129   _declared_nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields
130   _nonstatic_fields = nullptr;          // initialized lazily by compute_nonstatic_fields
131   _has_injected_fields = -1;
132   _is_hidden = false;
133   _is_record = false;
134   _loader = loader;
135   _is_shared = false;
136   _super = nullptr;
137   _java_mirror = nullptr;
138   _field_cache = nullptr;
139   _has_trusted_loader = compute_has_trusted_loader();
140 }
141 
142 
143 
144 // ------------------------------------------------------------------
145 // ciInstanceKlass::compute_shared_is_initialized
146 void ciInstanceKlass::compute_shared_init_state() {
147   GUARDED_VM_ENTRY(
148     InstanceKlass* ik = get_instanceKlass();
149     _init_state = ik->init_state();
150   )

308   if (name()->index_of_at(len+1, "/", 1) >= 0)
309     return false;
310 
311   return true;
312 }
313 
314 // ------------------------------------------------------------------
315 // ciInstanceKlass::print_impl
316 //
317 // Implementation of the print method.
318 void ciInstanceKlass::print_impl(outputStream* st) {
319   ciKlass::print_impl(st);
320   GUARDED_VM_ENTRY(st->print(" loader=" INTPTR_FORMAT, p2i(loader()));)
321   if (is_loaded()) {
322     st->print(" initialized=%s finalized=%s subklass=%s size=%d flags=",
323               bool_to_str(is_initialized()),
324               bool_to_str(has_finalizer()),
325               bool_to_str(has_subklass()),
326               layout_helper());
327 
328     _flags.print_klass_flags(st);
329 
330     if (_super) {
331       st->print(" super=");
332       _super->print_name_on(st);
333     }
334     if (_java_mirror) {
335       st->print(" mirror=PRESENT");
336     }
337   }
338 }
339 
340 // ------------------------------------------------------------------
341 // ciInstanceKlass::super
342 //
343 // Get the superklass of this klass.
344 ciInstanceKlass* ciInstanceKlass::super() {
345   assert(is_loaded(), "must be loaded");
346   if (_super == nullptr && !is_java_lang_Object()) {
347     GUARDED_VM_ENTRY(
348       Klass* super_klass = get_instanceKlass()->super();
349       _super = CURRENT_ENV->get_instance_klass(super_klass);
350     )
351   }
352   return _super;

374   if (!is_abstract())   return nullptr; // Only applies to abstract classes.
375   if (!has_subklass())  return nullptr; // Must have at least one subklass.
376   VM_ENTRY_MARK;
377   InstanceKlass* ik = get_instanceKlass();
378   Klass* up = ik->up_cast_abstract();
379   assert(up->is_instance_klass(), "must be InstanceKlass");
380   if (ik == up) {
381     return nullptr;
382   }
383   return CURRENT_THREAD_ENV->get_instance_klass(up);
384 }
385 
386 // ------------------------------------------------------------------
387 // ciInstanceKlass::has_finalizable_subclass
388 bool ciInstanceKlass::has_finalizable_subclass() {
389   if (!is_loaded())     return true;
390   VM_ENTRY_MARK;
391   return Dependencies::find_finalizable_subclass(get_instanceKlass()) != nullptr;
392 }
393 
394 bool ciInstanceKlass::contains_field_offset(int offset) const {


395   VM_ENTRY_MARK;
396   return get_instanceKlass()->contains_field_offset(offset);
397 }
398 
399 ciField* ciInstanceKlass::get_nonstatic_field_by_offset(const int field_offset) {
400   for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
401     ciField* field = nonstatic_field_at(i);
402     int field_off = field->offset_in_bytes();
403     if (field_off == field_offset) {
404       return field;
405     }
406   }
407   return nullptr;
408 }
409 
410 // ------------------------------------------------------------------
411 // ciInstanceKlass::get_field_by_offset
412 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
413   if (!is_static) {
414     return get_nonstatic_field_by_offset(field_offset);
415   }
416 
417   VM_ENTRY_MARK;
418   InstanceKlass* k = get_instanceKlass();
419   fieldDescriptor fd;
420   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
421     return nullptr;
422   }
423   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
424   return field;
425 }
426 
427 ciField* ciInstanceKlass::get_non_flat_field_by_offset(int field_offset) {
428   for (int i = 0, len = nof_declared_nonstatic_fields(); i < len; i++) {
429     ciField* field = declared_nonstatic_field_at(i);
430     int field_off = field->offset_in_bytes();
431     if (field_off == field_offset) {
432       return field;
433     }
434   }
435   return nullptr;
436 }
437 
438 int ciInstanceKlass::field_index_by_offset(int offset) {
439   int best_offset = 0;
440   int best_index = -1;
441   // Search the field with the given offset
442   for (int i = 0; i < nof_declared_nonstatic_fields(); ++i) {
443     int field_offset = declared_nonstatic_field_at(i)->offset_in_bytes();
444     if (field_offset == offset) {
445       // Exact match
446       return i;
447     } else if (field_offset < offset && field_offset > best_offset) {
448       // No exact match. Save the index of the field with the closest offset that
449       // is smaller than the given field offset. This index corresponds to the
450       // flat field that holds the field we are looking for.
451       best_offset = field_offset;
452       best_index = i;
453     }
454   }
455   assert(best_index >= 0, "field not found");
456   assert(best_offset == offset || declared_nonstatic_field_at(best_index)->type()->is_inlinetype(), "offset should match for non-inline types");
457   return best_index;
458 }
459 
460 // ------------------------------------------------------------------
461 // ciInstanceKlass::get_field_by_name
462 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
463   VM_ENTRY_MARK;
464   InstanceKlass* k = get_instanceKlass();
465   fieldDescriptor fd;
466   Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
467   if (def == nullptr) {
468     return nullptr;
469   }
470   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
471   return field;
472 }
473 
474 const GrowableArray<ciField*> empty_field_array(0, MemTag::mtCompiler);
475 
476 // This is essentially a shortcut for:
477 //   get_field_by_offset(field_offset, is_static)->layout_type()
478 // except this does not require allocating memory for a new ciField
479 BasicType ciInstanceKlass::get_field_type_by_offset(const int field_offset, const bool is_static) {
480   if (!is_static) {
481     ciField* field = get_nonstatic_field_by_offset(field_offset);
482     return field != nullptr ? field->layout_type() : T_ILLEGAL;
483   }
484 
485   // Avoid allocating a new ciField by obtaining the field type directly
486   VM_ENTRY_MARK;
487   InstanceKlass* k = get_instanceKlass();
488   fieldDescriptor fd;
489   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
490     return T_ILLEGAL;
491   }
492 
493   // Reproduce the behavior of ciField::layout_type
494   BasicType field_type = fd.field_type();
495   if (is_reference_type(field_type)) {
496     return T_OBJECT;
497   }
498   return type2field[make(field_type)->basic_type()];
499 }
500 
501 void ciInstanceKlass::compute_nonstatic_fields() {


502   assert(is_loaded(), "must be loaded");
503 
504   if (_nonstatic_fields != nullptr) {
505     assert(_declared_nonstatic_fields != nullptr, "must be initialized at the same time, class %s", name()->as_utf8());
506     return;
507   }
508 
509   if (!has_nonstatic_fields()) {
510     _declared_nonstatic_fields = &empty_field_array;
511     _nonstatic_fields = &empty_field_array;
512     return;
513   }
514   assert(!is_java_lang_Object(), "bootstrap OK");
515 
516   ciInstanceKlass* super = this->super();
517   assert(super != nullptr, "must have a super class, current class: %s", name()->as_utf8());
518   super->compute_nonstatic_fields();
519   const GrowableArray<ciField*>* super_declared_fields = super->_declared_nonstatic_fields;;
520   const GrowableArray<ciField*>* super_fields = super->_nonstatic_fields;
521   assert(super_declared_fields != nullptr && super_fields != nullptr, "must have been initialized, current class: %s, super class: %s", name()->as_utf8(), super->name()->as_utf8());

522 

523   GUARDED_VM_ENTRY({
524     compute_nonstatic_fields_impl(super_declared_fields, super_fields);
525   });















526 }
527 
528 void ciInstanceKlass::compute_nonstatic_fields_impl(const GrowableArray<ciField*>* super_declared_fields, const GrowableArray<ciField*>* super_fields) {
529   assert(_declared_nonstatic_fields == nullptr && _nonstatic_fields == nullptr, "initialized already");

530   ASSERT_IN_VM;
531   Arena* arena = CURRENT_ENV->arena();







532 
533   InstanceKlass* this_klass = get_instanceKlass();
534   int declared_field_num = 0;
535   int field_num = 0;
536   for (JavaFieldStream fs(this_klass); !fs.done(); fs.next()) {
537     if (fs.access_flags().is_static()) {
538       continue;
539     }
540 
541     declared_field_num++;
542 
543     fieldDescriptor& fd = fs.field_descriptor();
544     if (fd.is_flat()) {
545       InlineKlass* k = this_klass->get_inline_type_field_klass(fd.index());
546       ciInlineKlass* vk = CURRENT_ENV->get_klass(k)->as_inline_klass();
547       field_num += vk->nof_nonstatic_fields();
548       field_num += fd.has_null_marker() ? 1 : 0;
549     } else {
550       field_num++;
551     }
552   }
553 
554   GrowableArray<ciField*>* tmp_declared_fields = nullptr;
555   if (declared_field_num != 0) {
556     tmp_declared_fields = new (arena) GrowableArray<ciField*>(arena, declared_field_num + super_declared_fields->length(), 0, nullptr);
557     tmp_declared_fields->appendAll(super_declared_fields);
558   }
559 
560   GrowableArray<ciField*>* tmp_fields = nullptr;
561   if (field_num != 0) {
562     tmp_fields = new (arena) GrowableArray<ciField*>(arena, field_num + super_fields->length(), 0, nullptr);
563     tmp_fields->appendAll(super_fields);
564   }
565 
566   // For later assertion
567   declared_field_num += super_declared_fields->length();
568   field_num += super_fields->length();
569 
570   for (JavaFieldStream fs(this_klass); !fs.done(); fs.next()) {
571     if (fs.access_flags().is_static()) {
572       continue;
573     }
574 
575     fieldDescriptor& fd = fs.field_descriptor();
576     ciField* declared_field = new (arena) ciField(&fd);
577     assert(tmp_declared_fields != nullptr, "should be initialized");
578     tmp_declared_fields->append(declared_field);
579 
580     if (fd.is_flat()) {
581       // Flat fields are embedded
582       Klass* k = get_instanceKlass()->get_inline_type_field_klass(fd.index());
583       ciInlineKlass* vk = CURRENT_ENV->get_klass(k)->as_inline_klass();
584       // Iterate over fields of the flat inline type and copy them to 'this'
585       for (int i = 0; i < vk->nof_nonstatic_fields(); ++i) {
586         assert(tmp_fields != nullptr, "should be initialized");
587         tmp_fields->append(new (arena) ciField(declared_field, vk->nonstatic_field_at(i)));
588       }
589       if (fd.has_null_marker()) {
590         assert(tmp_fields != nullptr, "should be initialized");
591         tmp_fields->append(new (arena) ciField(declared_field));
592       }
593     } else {
594       assert(tmp_fields != nullptr, "should be initialized");
595       tmp_fields->append(declared_field);
596     }
597   }
598 
599   // Now sort them by offset, ascending. In principle, they could mix with superclass fields.
600   if (tmp_declared_fields != nullptr) {
601     assert(tmp_declared_fields->length() == declared_field_num, "sanity check failed for class: %s, number of declared fields: %d, expected: %d",
602            name()->as_utf8(), tmp_declared_fields->length(), declared_field_num);
603     _declared_nonstatic_fields = tmp_declared_fields;
604   } else {
605     _declared_nonstatic_fields = super_declared_fields;
606   }
607 
608   if (tmp_fields != nullptr) {
609     assert(tmp_fields->length() == field_num, "sanity check failed for class: %s, number of fields: %d, expected: %d",
610            name()->as_utf8(), tmp_fields->length(), field_num);
611     _nonstatic_fields = tmp_fields;
612   } else {
613     _nonstatic_fields = super_fields;
614   }


615 }
616 
617 bool ciInstanceKlass::compute_injected_fields_helper() {
618   ASSERT_IN_VM;
619   InstanceKlass* k = get_instanceKlass();
620 
621   for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
622     if (fs.access_flags().is_static())  continue;
623     return true;
624   }
625   return false;
626 }
627 
628 void ciInstanceKlass::compute_injected_fields() {
629   assert(is_loaded(), "must be loaded");
630 
631   int has_injected_fields = 0;
632   if (super() != nullptr && super()->has_injected_fields()) {
633     has_injected_fields = 1;
634   } else {

706     } else {
707       // Go into the VM to fetch the implementor.
708       VM_ENTRY_MARK;
709       InstanceKlass* ik = get_instanceKlass();
710       Klass* implk = ik->implementor();
711       if (implk != nullptr) {
712         if (implk == ik) {
713           // More than one implementors. Use 'this' in this case.
714           impl = this;
715         } else {
716           impl = CURRENT_THREAD_ENV->get_instance_klass(implk);
717         }
718       }
719     }
720     // Memoize this result.
721     _implementor = impl;
722   }
723   return impl;
724 }
725 
726 bool ciInstanceKlass::can_be_inline_klass(bool is_exact) {
727   if (!Arguments::is_valhalla_enabled()) {
728     return false;
729   }
730   if (!is_loaded() || is_inlinetype()) {
731     // Not loaded or known to be an inline klass
732     return true;
733   }
734   if (!is_exact) {
735     // Not exact, check if this is a valid super for an inline klass
736     GUARDED_VM_ENTRY(
737       return !get_instanceKlass()->access_flags().is_identity_class() || is_java_lang_Object();
738     )
739   }
740   return false;
741 }
742 
743 // Utility class for printing of the contents of the static fields for
744 // use by compilation replay.  It only prints out the information that
745 // could be consumed by the compiler, so for primitive types it prints
746 // out the actual value.  For Strings it's the actual string value.
747 // For array types it it's first level array size since that's the
748 // only value which statically unchangeable.  For all other reference
749 // types it simply prints out the dynamic type.
750 
751 class StaticFieldPrinter : public FieldClosure {
752 protected:
753   outputStream* _out;
754 public:
755   StaticFieldPrinter(outputStream* out) :
756     _out(out) {
757   }
758   void do_field_helper(fieldDescriptor* fd, oop obj, bool is_flat);
759 };
760 
761 class StaticFinalFieldPrinter : public StaticFieldPrinter {
762   const char*   _holder;
763  public:
764   StaticFinalFieldPrinter(outputStream* out, const char* holder) :
765     StaticFieldPrinter(out), _holder(holder) {

766   }
767   void do_field(fieldDescriptor* fd) {
768     if (fd->is_final() && !fd->has_initial_value()) {
769       ResourceMark rm;
770       InstanceKlass* holder = fd->field_holder();
771       oop mirror = holder->java_mirror();
772       _out->print("staticfield %s %s ", _holder, fd->name()->as_quoted_ascii());
773       BasicType bt = fd->field_type();
774       if (bt != T_OBJECT && bt != T_ARRAY) {
775         _out->print("%s ", fd->signature()->as_quoted_ascii());
776       }
777       do_field_helper(fd, mirror, false);
778       _out->cr();
779     }
780   }
781 };
782 
783 class InlineTypeFieldPrinter : public StaticFieldPrinter {
784   oop _obj;
785 public:
786   InlineTypeFieldPrinter(outputStream* out, oop obj) :
787     StaticFieldPrinter(out), _obj(obj) {
788   }
789   void do_field(fieldDescriptor* fd) {
790     do_field_helper(fd, _obj, true);
791     _out->print(" ");
792   }
793 };
794 
795 void StaticFieldPrinter::do_field_helper(fieldDescriptor* fd, oop mirror, bool is_flat) {
796   BasicType field_type = fd->field_type();
797   switch (field_type) {
798     case T_BYTE:    _out->print("%d", mirror->byte_field(fd->offset()));   break;
799     case T_BOOLEAN: _out->print("%d", mirror->bool_field(fd->offset()));   break;
800     case T_SHORT:   _out->print("%d", mirror->short_field(fd->offset()));  break;
801     case T_CHAR:    _out->print("%d", mirror->char_field(fd->offset()));   break;
802     case T_INT:     _out->print("%d", mirror->int_field(fd->offset()));    break;
803     case T_LONG:    _out->print(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset())));   break;
804     case T_FLOAT: {
805       float f = mirror->float_field(fd->offset());
806       _out->print("%d", *(int*)&f);
807       break;
808     }
809     case T_DOUBLE: {
810       double d = mirror->double_field(fd->offset());
811       _out->print(INT64_FORMAT, *(int64_t*)&d);
812       break;
813     }
814     case T_ARRAY:  // fall-through
815     case T_OBJECT:
816       if (!fd->is_null_free_inline_type()) {
817         _out->print("%s ", fd->signature()->as_quoted_ascii());
818         oop value =  mirror->obj_field_acquire(fd->offset());
819         if (value == nullptr) {
820           if (field_type == T_ARRAY) {
821             _out->print("%d", -1);
822           }
823           _out->cr();
824         } else if (value->is_instance()) {
825           assert(field_type == T_OBJECT, "");
826           if (value->is_a(vmClasses::String_klass())) {
827             const char* ascii_value = java_lang_String::as_quoted_ascii(value);
828             _out->print("\"%s\"", (ascii_value != nullptr) ? ascii_value : "");
829           } else {
830             const char* klass_name  = value->klass()->name()->as_quoted_ascii();
831             _out->print("%s", klass_name);
832           }
833         } else if (value->is_array()) {
834           arrayOop a = (arrayOop)value;
835           _out->print("%d", a->length());
836           if (value->is_objArray()) {
837             objArrayOop oa = (objArrayOop)value;
838             const char* klass_name  = value->klass()->name()->as_quoted_ascii();
839             _out->print(" %s", klass_name);
840           }
841         } else {
842           ShouldNotReachHere();
843         }
844         break;
845       } else {
846         // handling of null free inline type
847         ResetNoHandleMark rnhm;
848         Thread* THREAD = Thread::current();
849         SignatureStream ss(fd->signature(), false);
850         Symbol* name = ss.as_symbol();
851         assert(!HAS_PENDING_EXCEPTION, "can resolve klass?");
852         InstanceKlass* holder = fd->field_holder();
853         InstanceKlass* k = SystemDictionary::find_instance_klass(THREAD, name,
854                                                                  Handle(THREAD, holder->class_loader()));
855         guarantee(k != nullptr && !HAS_PENDING_EXCEPTION, "can resolve klass?");
856         InlineKlass* vk = InlineKlass::cast(k);
857         oop obj;
858         if (is_flat) {
859           int field_offset = fd->offset() - vk->payload_offset();
860           obj = cast_to_oop(cast_from_oop<address>(mirror) + field_offset);
861         } else {
862           obj = mirror->obj_field_acquire(fd->offset());
863         }
864         InlineTypeFieldPrinter print_field(_out, obj);
865         vk->do_nonstatic_fields(&print_field);
866         break;
867       }
868     default:
869       ShouldNotReachHere();
870   }
871 }
872 
873 const char *ciInstanceKlass::replay_name() const {
874   return CURRENT_ENV->replay_name(get_instanceKlass());
875 }
876 
877 void ciInstanceKlass::dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik) {
878   if (ik->is_hidden()) {
879     const char *name = CURRENT_ENV->dyno_name(ik);
880     if (name != nullptr) {
881       out->print_cr("instanceKlass %s # %s", name, ik->name()->as_quoted_ascii());
882     } else {
883       out->print_cr("# instanceKlass %s", ik->name()->as_quoted_ascii());
884     }
885   } else {
886     out->print_cr("instanceKlass %s", ik->name()->as_quoted_ascii());
887   }
888 }
889 
890 GrowableArray<ciInstanceKlass*>* ciInstanceKlass::transitive_interfaces() const{
891   if (_transitive_interfaces == nullptr) {
< prev index next >