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/ciCallSite.hpp"
 26 #include "ci/ciInstance.hpp"
 27 #include "ci/ciInstanceKlass.hpp"
 28 #include "ci/ciMemberName.hpp"
 29 #include "ci/ciMethod.hpp"
 30 #include "ci/ciMethodData.hpp"
 31 #include "ci/ciMethodHandle.hpp"
 32 #include "ci/ciMethodType.hpp"
 33 #include "ci/ciNullObject.hpp"
 34 #include "ci/ciObjArray.hpp"
 35 #include "ci/ciObjArrayKlass.hpp"
 36 #include "ci/ciObject.hpp"
 37 #include "ci/ciObjectFactory.hpp"
 38 #include "ci/ciReplay.hpp"
 39 #include "ci/ciSymbol.hpp"
 40 #include "ci/ciSymbols.hpp"
 41 #include "ci/ciTypeArray.hpp"
 42 #include "ci/ciTypeArrayKlass.hpp"
 43 #include "ci/ciUtilities.inline.hpp"
 44 #include "classfile/javaClasses.inline.hpp"
 45 #include "classfile/vmClasses.hpp"
 46 #include "compiler/compiler_globals.hpp"
 47 #include "compiler/compileTask.hpp"
 48 #include "gc/shared/collectedHeap.inline.hpp"
 49 #include "memory/allocation.inline.hpp"
 50 #include "memory/universe.hpp"
 51 #include "oops/instanceKlass.hpp"
 52 #include "oops/oop.inline.hpp"
 53 #include "oops/trainingData.hpp"
 54 #include "runtime/handles.inline.hpp"
 55 #include "runtime/signature.hpp"
 56 #include "utilities/macros.hpp"
 57 
 58 // ciObjectFactory
 59 //
 60 // This class handles requests for the creation of new instances
 61 // of ciObject and its subclasses.  It contains a caching mechanism
 62 // which ensures that for each oop, at most one ciObject is created.
 63 // This invariant allows more efficient implementation of ciObject.
 64 //
 65 // Implementation note: the oop->ciObject mapping is represented as
 66 // a table stored in an array.  Even though objects are moved
 67 // by the garbage collector, the compactor preserves their relative
 68 // order; address comparison of oops (in perm space) is safe so long
 69 // as we prohibit GC during our comparisons.  We currently use binary
 70 // search to find the oop in the table, and inserting a new oop
 71 // into the table may be costly.  If this cost ends up being
 72 // problematic the underlying data structure can be switched to some
 73 // sort of balanced binary tree.
 74 
 75 GrowableArray<ciMetadata*>* ciObjectFactory::_shared_ci_metadata = nullptr;
 76 ciSymbol*                 ciObjectFactory::_shared_ci_symbols[vmSymbols::number_of_symbols()];
 77 int                       ciObjectFactory::_shared_ident_limit = 0;
 78 volatile bool             ciObjectFactory::_initialized = false;
 79 
 80 
 81 // ------------------------------------------------------------------
 82 // ciObjectFactory::ciObjectFactory
 83 ciObjectFactory::ciObjectFactory(Arena* arena,
 84                                  int expected_size)
 85                                  : _arena(arena),
 86                                    _ci_metadata(arena, expected_size, 0, nullptr),
 87                                    _cached_init_state(arena, _shared_ident_limit, 0, (u1)0),
 88                                    _unloaded_methods(arena, 4, 0, nullptr),
 89                                    _unloaded_klasses(arena, 8, 0, nullptr),
 90                                    _unloaded_instances(arena, 4, 0, nullptr),
 91                                    _return_addresses(arena, 8, 0, nullptr),
 92                                    _symbols(arena, 100, 0, nullptr),
 93                                    _next_ident(_shared_ident_limit),
 94                                    _non_perm_count(0) {
 95   for (int i = 0; i < NON_PERM_BUCKETS; i++) {
 96     _non_perm_bucket[i] = nullptr;
 97   }
 98 
 99   // If the shared ci objects exist append them to this factory's objects
100   if (_shared_ci_metadata != nullptr) {
101     _ci_metadata.appendAll(_shared_ci_metadata);
102     // ciInstanceKlass for well-known class is shared by all
103     // compiler threads and can be updated concurrently by
104     // other compiler threads during compilation.
105     // Make local copy of class state to avoid state change
106     // during compilation.
107     int len = _ci_metadata.length();
108     for (int i = 0; i < len; i++) {
109       ciMetadata* obj = _ci_metadata.at(i);
110       if (obj->is_loaded() && obj->is_instance_klass()) {
111         ciInstanceKlass* cik = obj->as_instance_klass();
112         precond(cik->is_shared());
113         InstanceKlass::ClassState current_state = cik->_init_state;
114         InstanceKlass::ClassState state = InstanceKlass::fully_initialized;
115         if (current_state != state) {
116           GUARDED_VM_ENTRY( state = cik->get_instanceKlass()->init_state(); )
117           // Update state of shared ciInstanceKlass
118           cik->_init_state = state;
119         }
120         // Cache state for current compilation
121         _cached_init_state.at_put_grow(cik->ident(), (u1)state, 0);
122       }
123     }
124   }
125 }
126 
127 // ------------------------------------------------------------------
128 // ciObjectFactory::ciObjectFactory
129 void ciObjectFactory::initialize() {
130   ASSERT_IN_VM;
131   JavaThread* thread = JavaThread::current();
132   HandleMark  handle_mark(thread);
133 
134   // This Arena is long lived and exists in the resource mark of the
135   // compiler thread that initializes the initial ciObjectFactory which
136   // creates the shared ciObjects that all later ciObjectFactories use.
137   Arena* arena = new (mtCompiler) Arena(mtCompiler);
138   ciEnv initial(arena);
139   ciEnv* env = ciEnv::current();
140   env->_factory->init_shared_objects();
141 
142   _initialized = true;
143 
144 }
145 
146 void ciObjectFactory::init_shared_objects() {
147 
148   _next_ident = 1;  // start numbering CI objects at 1
149 
150   {
151     // Create the shared symbols, but not in _shared_ci_metadata.
152     for (auto index : EnumRange<vmSymbolID>{}) {
153       Symbol* vmsym = vmSymbols::symbol_at(index);
154       assert(vmSymbols::find_sid(vmsym) == index, "1-1 mapping");
155       ciSymbol* sym = new (_arena) ciSymbol(vmsym, index);
156       init_ident_of(sym);
157       _shared_ci_symbols[vmSymbols::as_int(index)] = sym;
158     }
159 #ifdef ASSERT
160     for (auto index : EnumRange<vmSymbolID>{}) {
161       Symbol* vmsym = vmSymbols::symbol_at(index);
162       ciSymbol* sym = vm_symbol_at(index);
163       assert(sym->get_symbol() == vmsym, "oop must match");
164     }
165     assert(ciSymbols::void_class_signature()->get_symbol() == vmSymbols::void_class_signature(), "spot check");
166 #endif
167   }
168 
169   for (int i = T_BOOLEAN; i <= T_CONFLICT; i++) {
170     BasicType t = (BasicType)i;
171     if (type2name(t) != nullptr && !is_reference_type(t) &&
172         t != T_NARROWOOP && t != T_NARROWKLASS) {
173       ciType::_basic_types[t] = new (_arena) ciType(t);
174       init_ident_of(ciType::_basic_types[t]);
175     }
176   }
177 
178   ciEnv::_null_object_instance = new (_arena) ciNullObject();
179   init_ident_of(ciEnv::_null_object_instance);
180 
181 #define VM_CLASS_DEFN(name, ignore_s)                              \
182   if (vmClasses::name##_is_loaded()) \
183     ciEnv::_##name = get_metadata(vmClasses::name())->as_instance_klass();
184 
185   VM_CLASSES_DO(VM_CLASS_DEFN)
186 #undef VM_CLASS_DEFN
187 
188   for (int len = -1; len != _ci_metadata.length(); ) {
189     len = _ci_metadata.length();
190     for (int i2 = 0; i2 < len; i2++) {
191       ciMetadata* obj = _ci_metadata.at(i2);
192       assert (obj->is_metadata(), "what else would it be?");
193       if (obj->is_loaded() && obj->is_instance_klass()) {
194         obj->as_instance_klass()->compute_nonstatic_fields();
195         obj->as_instance_klass()->transitive_interfaces();
196       }
197     }
198   }
199 
200   ciEnv::_unloaded_cisymbol = ciObjectFactory::get_symbol(vmSymbols::dummy_symbol());
201   // Create dummy InstanceKlass and ObjArrayKlass object and assign them idents
202   ciEnv::_unloaded_ciinstance_klass = new (_arena) ciInstanceKlass(ciEnv::_unloaded_cisymbol, nullptr);
203   init_ident_of(ciEnv::_unloaded_ciinstance_klass);
204   ciEnv::_unloaded_ciobjarrayklass = new (_arena) ciObjArrayKlass(ciEnv::_unloaded_cisymbol, ciEnv::_unloaded_ciinstance_klass, 1);
205   init_ident_of(ciEnv::_unloaded_ciobjarrayklass);
206   assert(ciEnv::_unloaded_ciobjarrayklass->is_obj_array_klass(), "just checking");
207 
208   get_metadata(Universe::boolArrayKlass());
209   get_metadata(Universe::charArrayKlass());
210   get_metadata(Universe::floatArrayKlass());
211   get_metadata(Universe::doubleArrayKlass());
212   get_metadata(Universe::byteArrayKlass());
213   get_metadata(Universe::shortArrayKlass());
214   get_metadata(Universe::intArrayKlass());
215   get_metadata(Universe::longArrayKlass());
216 
217   assert(_non_perm_count == 0, "no shared non-perm objects");
218 
219   // The shared_ident_limit is the first ident number that will
220   // be used for non-shared objects.  That is, numbers less than
221   // this limit are permanently assigned to shared CI objects,
222   // while the higher numbers are recycled afresh by each new ciEnv.
223 
224   _shared_ident_limit = _next_ident;
225   _shared_ci_metadata = &_ci_metadata;
226 }
227 
228 
229 ciSymbol* ciObjectFactory::get_symbol(Symbol* key) {
230   vmSymbolID sid = vmSymbols::find_sid(key);
231   if (sid != vmSymbolID::NO_SID) {
232     // do not pollute the main cache with it
233     return vm_symbol_at(sid);
234   }
235 
236   assert(vmSymbols::find_sid(key) == vmSymbolID::NO_SID, "");
237   ciSymbol* s = new (arena()) ciSymbol(key, vmSymbolID::NO_SID);
238   _symbols.push(s);
239   return s;
240 }
241 
242 // Decrement the refcount when done on symbols referenced by this compilation.
243 void ciObjectFactory::remove_symbols() {
244   for (int i = 0; i < _symbols.length(); i++) {
245     ciSymbol* s = _symbols.at(i);
246     s->get_symbol()->decrement_refcount();
247   }
248   // Since _symbols is resource allocated we're not allowed to delete it
249   // but it'll go away just the same.
250 }
251 
252 // ------------------------------------------------------------------
253 // ciObjectFactory::get
254 //
255 // Get the ciObject corresponding to some oop.  If the ciObject has
256 // already been created, it is returned.  Otherwise, a new ciObject
257 // is created.
258 ciObject* ciObjectFactory::get(oop key) {
259   ASSERT_IN_VM;
260 
261   Handle keyHandle(Thread::current(), key);
262   assert(Universe::heap()->is_in(keyHandle()), "must be");
263 
264   NonPermObject* &bucket = find_non_perm(keyHandle);
265   if (bucket != nullptr) {
266     return bucket->object();
267   }
268 
269   // The ciObject does not yet exist.  Create it and insert it
270   // into the cache.
271   ciObject* new_object = create_new_object(keyHandle());
272   assert(keyHandle() == new_object->get_oop(), "must be properly recorded");
273   init_ident_of(new_object);
274   assert(Universe::heap()->is_in(new_object->get_oop()), "must be");
275 
276   // Not a perm-space object.
277   insert_non_perm(bucket, keyHandle, new_object);
278   notice_new_object(new_object);
279   return new_object;
280 }
281 
282 void ciObjectFactory::notice_new_object(ciBaseObject* new_object) {
283   if (TrainingData::need_data()) {
284     ciEnv* env = ciEnv::current();
285     if (env->task() != nullptr) {
286       // Note: task will be null during init_compiler_runtime.
287       CompileTrainingData* td = env->task()->training_data();
288       if (td != nullptr) {
289         td->notice_jit_observation(env, new_object);
290       }
291     }
292   }
293 }
294 
295 int ciObjectFactory::metadata_compare(Metadata* const& key, ciMetadata* const& elt) {
296   Metadata* value = elt->constant_encoding();
297   if (key < value)      return -1;
298   else if (key > value) return 1;
299   else                  return 0;
300 }
301 
302 // ------------------------------------------------------------------
303 // ciObjectFactory::cached_metadata
304 //
305 // Get the ciMetadata corresponding to some Metadata. If the ciMetadata has
306 // already been created, it is returned. Otherwise, null is returned.
307 ciMetadata* ciObjectFactory::cached_metadata(Metadata* key) {
308   ASSERT_IN_VM;
309 
310   bool found = false;
311   int index = _ci_metadata.find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
312 
313   if (!found) {
314     return nullptr;
315   }
316   return _ci_metadata.at(index)->as_metadata();
317 }
318 
319 
320 // ------------------------------------------------------------------
321 // ciObjectFactory::get_metadata
322 //
323 // Get the ciMetadata corresponding to some Metadata. If the ciMetadata has
324 // already been created, it is returned. Otherwise, a new ciMetadata
325 // is created.
326 ciMetadata* ciObjectFactory::get_metadata(Metadata* key) {
327   ASSERT_IN_VM;
328 
329   if (ReplayCompiles && key->is_klass()) {
330     Klass* k = (Klass*)key;
331     if (k->is_instance_klass() && ciReplay::is_klass_unresolved((InstanceKlass*)k)) {
332       // Klass was unresolved at replay dump time. Simulate this case.
333       return ciEnv::_unloaded_ciinstance_klass;
334     }
335   }
336 
337 #ifdef ASSERT
338   if (CIObjectFactoryVerify) {
339     Metadata* last = nullptr;
340     for (int j = 0; j < _ci_metadata.length(); j++) {
341       Metadata* o = _ci_metadata.at(j)->constant_encoding();
342       assert(last < o, "out of order");
343       last = o;
344     }
345   }
346 #endif // ASSERT
347   int len = _ci_metadata.length();
348   bool found = false;
349   int index = _ci_metadata.find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
350 #ifdef ASSERT
351   if (CIObjectFactoryVerify) {
352     for (int i = 0; i < _ci_metadata.length(); i++) {
353       if (_ci_metadata.at(i)->constant_encoding() == key) {
354         assert(index == i, " bad lookup");
355       }
356     }
357   }
358 #endif
359 
360   if (!found) {
361     // The ciMetadata does not yet exist. Create it and insert it
362     // into the cache.
363     ciMetadata* new_object = create_new_metadata(key);
364     init_ident_of(new_object);
365     assert(new_object->is_metadata(), "must be");
366 
367     if (len != _ci_metadata.length()) {
368       // creating the new object has recursively entered new objects
369       // into the table.  We need to recompute our index.
370       index = _ci_metadata.find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
371     }
372     assert(!found, "no double insert");
373     _ci_metadata.insert_before(index, new_object);
374     notice_new_object(new_object);
375     return new_object;
376   }
377   return _ci_metadata.at(index)->as_metadata();
378 }
379 
380 // ------------------------------------------------------------------
381 // ciObjectFactory::create_new_object
382 //
383 // Create a new ciObject from an oop.
384 //
385 // Implementation note: this functionality could be virtual behavior
386 // of the oop itself.  For now, we explicitly marshal the object.
387 ciObject* ciObjectFactory::create_new_object(oop o) {
388   EXCEPTION_CONTEXT;
389 
390   if (o->is_instance()) {
391     instanceHandle h_i(THREAD, (instanceOop)o);
392     if (java_lang_invoke_CallSite::is_instance(o))
393       return new (arena()) ciCallSite(h_i);
394     else if (java_lang_invoke_MemberName::is_instance(o))
395       return new (arena()) ciMemberName(h_i);
396     else if (java_lang_invoke_MethodHandle::is_instance(o))
397       return new (arena()) ciMethodHandle(h_i);
398     else if (java_lang_invoke_MethodType::is_instance(o))
399       return new (arena()) ciMethodType(h_i);
400     else
401       return new (arena()) ciInstance(h_i);
402   } else if (o->is_objArray()) {
403     objArrayHandle h_oa(THREAD, (objArrayOop)o);
404     return new (arena()) ciObjArray(h_oa);
405   } else if (o->is_typeArray()) {
406     typeArrayHandle h_ta(THREAD, (typeArrayOop)o);
407     return new (arena()) ciTypeArray(h_ta);
408   }
409 
410   // The oop is of some type not supported by the compiler interface.
411   ShouldNotReachHere();
412   return nullptr;
413 }
414 
415 // ------------------------------------------------------------------
416 // ciObjectFactory::create_new_metadata
417 //
418 // Create a new ciMetadata from a Metadata*.
419 //
420 // Implementation note: in order to keep Metadata live, an auxiliary ciObject
421 // is used, which points to it's holder.
422 ciMetadata* ciObjectFactory::create_new_metadata(Metadata* o) {
423   EXCEPTION_CONTEXT;
424 
425   if (o->is_klass()) {
426     Klass* k = (Klass*)o;
427     if (k->is_instance_klass()) {
428       assert(!ReplayCompiles || ciReplay::no_replay_state() || !ciReplay::is_klass_unresolved((InstanceKlass*)k), "must be whitelisted for replay compilation");
429       return new (arena()) ciInstanceKlass(k);
430     } else if (k->is_objArray_klass()) {
431       return new (arena()) ciObjArrayKlass(k);
432     } else if (k->is_typeArray_klass()) {
433       return new (arena()) ciTypeArrayKlass(k);
434     }
435   } else if (o->is_method()) {
436     methodHandle h_m(THREAD, (Method*)o);
437     ciEnv *env = CURRENT_THREAD_ENV;
438     ciInstanceKlass* holder = env->get_instance_klass(h_m()->method_holder());
439     return new (arena()) ciMethod(h_m, holder);
440   } else if (o->is_methodData()) {
441     // Hold methodHandle alive - might not be necessary ???
442     methodHandle h_m(THREAD, ((MethodData*)o)->method());
443     return new (arena()) ciMethodData((MethodData*)o);
444   }
445 
446   // The Metadata* is of some type not supported by the compiler interface.
447   ShouldNotReachHere();
448   return nullptr;
449 }
450 
451 //------------------------------------------------------------------
452 // ciObjectFactory::get_unloaded_method
453 //
454 // Get the ciMethod representing an unloaded/unfound method.
455 //
456 // Implementation note: unloaded methods are currently stored in
457 // an unordered array, requiring a linear-time lookup for each
458 // unloaded method.  This may need to change.
459 ciMethod* ciObjectFactory::get_unloaded_method(ciInstanceKlass* holder,
460                                                ciSymbol*        name,
461                                                ciSymbol*        signature,
462                                                ciInstanceKlass* accessor) {
463   assert(accessor != nullptr, "need origin of access");
464   ciSignature* that = nullptr;
465   for (int i = 0; i < _unloaded_methods.length(); i++) {
466     ciMethod* entry = _unloaded_methods.at(i);
467     if (entry->holder()->equals(holder) &&
468         entry->name()->equals(name) &&
469         entry->signature()->as_symbol()->equals(signature)) {
470       // Short-circuit slow resolve.
471       if (entry->signature()->accessing_klass() == accessor) {
472         // We've found a match.
473         return entry;
474       } else {
475         // Lazily create ciSignature
476         if (that == nullptr)  that = new (arena()) ciSignature(accessor, constantPoolHandle(), signature);
477         if (entry->signature()->equals(that)) {
478           // We've found a match.
479           return entry;
480         }
481       }
482     }
483   }
484 
485   // This is a new unloaded method.  Create it and stick it in
486   // the cache.
487   ciMethod* new_method = new (arena()) ciMethod(holder, name, signature, accessor);
488 
489   init_ident_of(new_method);
490   _unloaded_methods.append(new_method);
491 
492   return new_method;
493 }
494 
495 //------------------------------------------------------------------
496 // ciObjectFactory::get_unloaded_klass
497 //
498 // Get a ciKlass representing an unloaded klass.
499 //
500 // Implementation note: unloaded klasses are currently stored in
501 // an unordered array, requiring a linear-time lookup for each
502 // unloaded klass.  This may need to change.
503 ciKlass* ciObjectFactory::get_unloaded_klass(ciKlass* accessing_klass,
504                                              ciSymbol* name,
505                                              bool create_if_not_found) {
506   EXCEPTION_CONTEXT;
507   oop loader = nullptr;
508   oop domain = nullptr;
509   if (accessing_klass != nullptr) {
510     loader = accessing_klass->loader();
511   }
512   for (int i = 0; i < _unloaded_klasses.length(); i++) {
513     ciKlass* entry = _unloaded_klasses.at(i);
514     if (entry->name()->equals(name) &&
515         entry->loader() == loader) {
516       // We've found a match.
517       return entry;
518     }
519   }
520 
521   if (!create_if_not_found)
522     return nullptr;
523 
524   // This is a new unloaded klass.  Create it and stick it in
525   // the cache.
526   ciKlass* new_klass = nullptr;
527 
528   // Two cases: this is an unloaded ObjArrayKlass or an
529   // unloaded InstanceKlass.  Deal with both.
530   if (name->char_at(0) == JVM_SIGNATURE_ARRAY) {
531     // Decompose the name.'
532     SignatureStream ss(name->get_symbol(), false);
533     int dimension = ss.skip_array_prefix();  // skip all '['s
534     BasicType element_type = ss.type();
535     assert(element_type != T_ARRAY, "unsuccessful decomposition");
536     ciKlass* element_klass = nullptr;
537     if (element_type == T_OBJECT) {
538       ciEnv *env = CURRENT_THREAD_ENV;
539       ciSymbol* ci_name = env->get_symbol(ss.as_symbol());
540       element_klass =
541         env->get_klass_by_name(accessing_klass, ci_name, false)->as_instance_klass();
542     } else {
543       assert(dimension > 1, "one dimensional type arrays are always loaded.");
544 
545       // The type array itself takes care of one of the dimensions.
546       dimension--;
547 
548       // The element klass is a TypeArrayKlass.
549       element_klass = ciTypeArrayKlass::make(element_type);
550     }
551     new_klass = new (arena()) ciObjArrayKlass(name, element_klass, dimension);
552   } else {
553     jobject loader_handle = nullptr;
554     if (accessing_klass != nullptr) {
555       loader_handle = accessing_klass->loader_handle();
556     }
557     new_klass = new (arena()) ciInstanceKlass(name, loader_handle);
558   }
559   init_ident_of(new_klass);
560   _unloaded_klasses.append(new_klass);
561 
562   return new_klass;
563 }
564 
565 
566 //------------------------------------------------------------------
567 // ciObjectFactory::get_unloaded_instance
568 //
569 // Get a ciInstance representing an as-yet undetermined instance of a given class.
570 //
571 ciInstance* ciObjectFactory::get_unloaded_instance(ciInstanceKlass* instance_klass) {
572   for (int i = 0; i < _unloaded_instances.length(); i++) {
573     ciInstance* entry = _unloaded_instances.at(i);
574     if (entry->klass()->equals(instance_klass)) {
575       // We've found a match.
576       return entry;
577     }
578   }
579 
580   // This is a new unloaded instance.  Create it and stick it in
581   // the cache.
582   ciInstance* new_instance = new (arena()) ciInstance(instance_klass);
583 
584   init_ident_of(new_instance);
585   _unloaded_instances.append(new_instance);
586 
587   // make sure it looks the way we want:
588   assert(!new_instance->is_loaded(), "");
589   assert(new_instance->klass() == instance_klass, "");
590 
591   return new_instance;
592 }
593 
594 
595 //------------------------------------------------------------------
596 // ciObjectFactory::get_unloaded_klass_mirror
597 //
598 // Get a ciInstance representing an unresolved klass mirror.
599 //
600 // Currently, this ignores the parameters and returns a unique unloaded instance.
601 ciInstance* ciObjectFactory::get_unloaded_klass_mirror(ciKlass* type) {
602   assert(ciEnv::_Class_klass != nullptr, "");
603   return get_unloaded_instance(ciEnv::_Class_klass->as_instance_klass());
604 }
605 
606 //------------------------------------------------------------------
607 // ciObjectFactory::get_unloaded_method_handle_constant
608 //
609 // Get a ciInstance representing an unresolved method handle constant.
610 //
611 // Currently, this ignores the parameters and returns a unique unloaded instance.
612 ciInstance* ciObjectFactory::get_unloaded_method_handle_constant(ciKlass*  holder,
613                                                                  ciSymbol* name,
614                                                                  ciSymbol* signature,
615                                                                  int       ref_kind) {
616   assert(ciEnv::_MethodHandle_klass != nullptr, "");
617   return get_unloaded_instance(ciEnv::_MethodHandle_klass->as_instance_klass());
618 }
619 
620 //------------------------------------------------------------------
621 // ciObjectFactory::get_unloaded_method_type_constant
622 //
623 // Get a ciInstance representing an unresolved method type constant.
624 //
625 // Currently, this ignores the parameters and returns a unique unloaded instance.
626 ciInstance* ciObjectFactory::get_unloaded_method_type_constant(ciSymbol* signature) {
627   assert(ciEnv::_MethodType_klass != nullptr, "");
628   return get_unloaded_instance(ciEnv::_MethodType_klass->as_instance_klass());
629 }
630 
631 ciInstance* ciObjectFactory::get_unloaded_object_constant() {
632   assert(ciEnv::_Object_klass != nullptr, "");
633   return get_unloaded_instance(ciEnv::_Object_klass->as_instance_klass());
634 }
635 
636 //------------------------------------------------------------------
637 // ciObjectFactory::get_empty_methodData
638 //
639 // Get the ciMethodData representing the methodData for a method with
640 // none.
641 ciMethodData* ciObjectFactory::get_empty_methodData() {
642   ciMethodData* new_methodData = new (arena()) ciMethodData();
643   init_ident_of(new_methodData);
644   return new_methodData;
645 }
646 
647 //------------------------------------------------------------------
648 // ciObjectFactory::get_return_address
649 //
650 // Get a ciReturnAddress for a specified bci.
651 ciReturnAddress* ciObjectFactory::get_return_address(int bci) {
652   for (int i = 0; i < _return_addresses.length(); i++) {
653     ciReturnAddress* entry = _return_addresses.at(i);
654     if (entry->bci() == bci) {
655       // We've found a match.
656       return entry;
657     }
658   }
659 
660   ciReturnAddress* new_ret_addr = new (arena()) ciReturnAddress(bci);
661   init_ident_of(new_ret_addr);
662   _return_addresses.append(new_ret_addr);
663   return new_ret_addr;
664 }
665 
666 // ------------------------------------------------------------------
667 // ciObjectFactory::init_ident_of
668 void ciObjectFactory::init_ident_of(ciBaseObject* obj) {
669   obj->set_ident(_next_ident++);
670 }
671 
672 static ciObjectFactory::NonPermObject* emptyBucket = nullptr;
673 
674 // ------------------------------------------------------------------
675 // ciObjectFactory::find_non_perm
676 //
677 // Use a small hash table, hashed on the klass of the key.
678 // If there is no entry in the cache corresponding to this oop, return
679 // the null tail of the bucket into which the oop should be inserted.
680 ciObjectFactory::NonPermObject* &ciObjectFactory::find_non_perm(Handle keyHandle) {
681   assert(Universe::heap()->is_in(keyHandle()), "must be");
682   ciMetadata* klass = get_metadata(keyHandle->klass()); // This may safepoint!
683   NonPermObject* *bp = &_non_perm_bucket[(unsigned) klass->hash() % NON_PERM_BUCKETS];
684   for (NonPermObject* p; (p = (*bp)) != nullptr; bp = &p->next()) {
685     if (is_equal(p, keyHandle()))  break;
686   }
687   return (*bp);
688 }
689 
690 
691 
692 // ------------------------------------------------------------------
693 // Code for for NonPermObject
694 //
695 inline ciObjectFactory::NonPermObject::NonPermObject(ciObjectFactory::NonPermObject* &bucket, oop key, ciObject* object) {
696   assert(ciObjectFactory::is_initialized(), "");
697   _object = object;
698   _next = bucket;
699   bucket = this;
700 }
701 
702 
703 
704 // ------------------------------------------------------------------
705 // ciObjectFactory::insert_non_perm
706 //
707 // Insert a ciObject into the non-perm table.
708 void ciObjectFactory::insert_non_perm(ciObjectFactory::NonPermObject* &where, Handle keyHandle, ciObject* obj) {
709   assert(Universe::heap()->is_in_or_null(keyHandle()), "must be");
710   assert(&where != &emptyBucket, "must not try to fill empty bucket");
711   NonPermObject* p = new (arena()) NonPermObject(where, keyHandle(), obj);
712   assert(where == p && is_equal(p, keyHandle()) && p->object() == obj, "entry must match");
713   assert(find_non_perm(keyHandle) == p, "must find the same spot");
714   ++_non_perm_count;
715 }
716 
717 // ------------------------------------------------------------------
718 // ciObjectFactory::vm_symbol_at
719 // Get the ciSymbol corresponding to some index in vmSymbols.
720 ciSymbol* ciObjectFactory::vm_symbol_at(vmSymbolID sid) {
721   int index = vmSymbols::as_int(sid);
722   return _shared_ci_symbols[index];
723 }
724 
725 // ------------------------------------------------------------------
726 // ciObjectFactory::metadata_do
727 void ciObjectFactory::metadata_do(MetadataClosure* f) {
728   for (int j = 0; j < _ci_metadata.length(); j++) {
729     Metadata* o = _ci_metadata.at(j)->constant_encoding();
730     f->do_metadata(o);
731   }
732 }
733 
734 // ------------------------------------------------------------------
735 // ciObjectFactory::print_contents_impl
736 void ciObjectFactory::print_contents_impl() {
737   int len = _ci_metadata.length();
738   tty->print_cr("ciObjectFactory (%d) meta data contents:", len);
739   for (int i = 0; i < len; i++) {
740     _ci_metadata.at(i)->print();
741     tty->cr();
742   }
743 }
744 
745 // ------------------------------------------------------------------
746 // ciObjectFactory::print_contents
747 void ciObjectFactory::print_contents() {
748   print();
749   tty->cr();
750   GUARDED_VM_ENTRY(print_contents_impl();)
751 }
752 
753 // ------------------------------------------------------------------
754 // ciObjectFactory::print
755 //
756 // Print debugging information about the object factory
757 void ciObjectFactory::print() {
758   tty->print("<ciObjectFactory oops=%d metadata=%d unloaded_methods=%d unloaded_instances=%d unloaded_klasses=%d>",
759              _non_perm_count, _ci_metadata.length(), _unloaded_methods.length(),
760              _unloaded_instances.length(),
761              _unloaded_klasses.length());
762 }